Skip to content

Instantly share code, notes, and snippets.

@niksumeiko
Last active November 10, 2024 18:26
Show Gist options
  • Save niksumeiko/8972566 to your computer and use it in GitHub Desktop.
Save niksumeiko/8972566 to your computer and use it in GitHub Desktop.
Moving git repository and all its branches, tags to a new remote repository keeping commits history
#!/bin/bash
# Sometimes you need to move your existing git repository
# to a new remote repository (/new remote origin).
# Here are a simple and quick steps that does exactly this.
#
# Let's assume we call "old repo" the repository you wish
# to move, and "new repo" the one you wish to move to.
#
### Step 1. Make sure you have a local copy of all "old repo"
### branches and tags.
# Fetch all of the remote branches and tags:
git fetch origin
# View all "old repo" local and remote branches:
git branch -a
# If some of the remotes/ branches doesn't have a local copy,
# checkout to create a local copy of the missing ones:
git checkout -b <branch> origin/<branch>
# Now we have to have all remote branches locally.
### Step 2. Add a "new repo" as a new remote origin:
git remote add new-origin [email protected]:user/repo.git
### Step 3. Push all local branches and tags to a "new repo".
# Push all local branches (note we're pushing to new-origin):
git push --all new-origin
# Push all tags:
git push --tags new-origin
### Step 4. Remove "old repo" origin and its dependencies.
# View existing remotes (you'll see 2 remotes for both fetch and push)
git remote -v
# Remove "old repo" remote:
git remote rm origin
# Rename "new repo" remote into just 'origin':
git remote rename new-origin origin
### Done! Now your local git repo is connected to "new repo" remote
### which has all the branches, tags and commits history.
@ganginenivenu
Copy link

git erro

@ravindrabhandarkar007
Copy link

ravindrabhandarkar007 commented Oct 21, 2019 via email

@Yoges-hatched
Copy link

Thanks @niksumeiko

@viktor32
Copy link

git clone --bare {repo}
cd {project}.git
git push --mirror {new-repo}

@monil1334
Copy link

@niksumeiko one question is that how would the other developers that already have existing git repo linked with the old repo URL ? how can they migrate their origin ?

@niksumeiko
Copy link
Author

niksumeiko commented May 7, 2020

@monil1334, other developers that already have an existing repo, shall only update their origin to a new url:

# Set a "new repo" url to remote origin:
git remote set-url [email protected]:user/repo.git

Now every developer can continue to pull/push as it used to be. A code will be retrieved and sent to a new repository.

@ravindrabhandarkar007
Copy link

ravindrabhandarkar007 commented May 7, 2020 via email

@swati-patel
Copy link

Thanks for the clear instructions!

@danascape
Copy link

if I already have a mirror pushed. and there are some updates to the original repository, will the same method update all the branches ?

@abhcr
Copy link

abhcr commented Sep 28, 2020

Personally I think that it's much better to do it this way:

git clone --mirror URL
git remote add NEW-REMOTE URL
git push  NEW-REMOTE --mirror 

This works only if you have all commits pushed into the old repo. In my case, I was working on a public repo with some local commits and once I found it good, had to create a new repo of my own (but including my local commits and branches on top of the old repo). The sequence of steps mentioned by the OP helped me with that.

@Rasfuranku
Copy link

Works like a charm!

@vioKamran
Copy link

I want Transfer git repositories from Bonobo.Git.Server to GitHub

@chriswycoff
Copy link

Than you so much

@astiw2
Copy link

astiw2 commented Aug 5, 2022

@niksumeiko Hi! I need to migrate from GH Enterprise to GH Enterprise Cloud. Is there a possibility to migrate all the closed Pull Requests as well? I can't find any GH REST API that helps(for obvious reasons I guess). Do you guys feel that if the code is merged and the base branches are already deleted why do we need to migrate those closed PR#s?

//Ashish

@niksumeiko
Copy link
Author

niksumeiko commented Aug 23, 2022

@niksumeiko Hi! I need to migrate from GH Enterprise to GH Enterprise Cloud. Is there a possibility to migrate all the closed Pull Requests as well? I can't find any GH REST API that helps(for obvious reasons I guess). Do you guys feel that if the code is merged and the base branches are already deleted why do we need to migrate those closed PR#s?

@astiw2, there's totally a way to do GH Enterprise migration (incl. all pull requests, issues…), but not via the git command line. Please consult with GH enterprise support people. They serve us wonderful. We're also on their Enterprise tyre.

@LameckMeshack
Copy link

LameckMeshack commented Jan 26, 2023

Personally I think that it's much better to do it this way:

git clone --mirror URL
git remote add NEW-REMOTE URL
git push  NEW-REMOTE --mirror 

@prashathsenthil You have to run these commands as below

git clone --mirror <url_of_old_repo>
cd <name_of_old_repo>
git remote add new-origin <url_of_new_repo>
git push new-origin --mirror

Thanks very much, I do it in this way , migrate all the branches in the repository from gerrit to gitlab successfuuly.

can this work for Gitlab? I have a branch on a Company Gitlab project and I would like to see my contributions on my GitHub?

@jhngrn
Copy link

jhngrn commented Jul 20, 2023

Is there a method to pull from git and migrate elsewhere such as svn for instance on a local repo with history? I've searched these internet pages a few months now and not much has come up.

@atosscsd-bb
Copy link

git clone --bare {repo}
cd {project}.git
git push --mirror {new-repo}

This is the way.

@Fmstrat
Copy link

Fmstrat commented Nov 10, 2024

Please no one do this. And unlike above comment, should use mirror the whole time.

git clone --mirror <URL to OLD repo location>
cd <New directory where OLD repo was cloned>
git remote set-url origin <URL to NEW repo location>
git push --mirror origin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment