Skip to content

Instantly share code, notes, and snippets.

@niksumeiko
Last active April 30, 2024 12:54
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 git@github.com: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.
@niksumeiko
Copy link
Author

niksumeiko commented Sep 26, 2019

@ravindrabhandarkar007, your branches are new, because they were migrated from an old repository and pushed to a new repository along with the rest. If you keep working on your main branches, these won't popup for pull request anymore.

@ravindrabhandarkar007
Copy link

ravindrabhandarkar007 commented Sep 26, 2019 via email

@niksumeiko
Copy link
Author

When you migrate, you push everything from your local to a new repository (new "remote" in git terms). Old branches are considered new by a new remote because they were pushed for a first time.

@ravindrabhandarkar007
Copy link

ravindrabhandarkar007 commented Sep 27, 2019 via email

@ravindrabhandarkar007
Copy link

ravindrabhandarkar007 commented Sep 27, 2019 via email

@ravindrabhandarkar007
Copy link

ravindrabhandarkar007 commented Sep 30, 2019 via email

@liyongjun1
Copy link

waiting for your reply On Fri, Sep 27, 2019 at 12:00 PM ravindra bhandarkar < ravindrabhandarkar007@gmail.com> wrote:

Can we fork it? is it a good practice? is there any other way? On Fri, Sep 27, 2019 at 11:49 AM ravindra bhandarkar < @.> wrote: > cant we prevent that scenario. cant we prevent it from happening? > I want them to be treated as old branches only.help me out? > > On Thu, Sep 26, 2019 at 1:35 PM Nik Sumeiko @.> > wrote: > >> When you migrate, you push everything from your local to a new >> repository (new "remote" in git terms). Old branches are considered new by >> a new remote because they were pushed for a first time. >> >> — >> You are receiving this because you were mentioned. >> Reply to this email directly, view it on GitHub >> https://gist.github.com/8972566?email_source=notifications&email_token=ALIO4IDZPNVQONIOYJYUJ4TQLRUOBA5CNFSM4HW7NI3KYY3PNVWWK3TUL52HS4DFVNDWS43UINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQAFZNRU#gistcomment-3037978, >> or mute the thread >> https://github.com/notifications/unsubscribe-auth/ALIO4IENPGLSBY2DMW6NOKDQLRUOBANCNFSM4HW7NI3A >> . >> >

if you want to migrate the reponsitory. you must use --bare or --mirror option to clone it. then push it to the new remote reponsitory. Then all the branches will be migrate to the new repnsitory.

@niksumeiko
Copy link
Author

Doesn't matter how we migrate, as soon as we push old branches to a new remote they will be considered as new once and Github will offer creating pull requests out them. But this Github notification is private to an author, so no one else sees them. As well, they will disappear when you keep working/pushing to your main branches within time. @ravindrabhandarkar007, why are you worrying?

@ravindrabhandarkar007
Copy link

ravindrabhandarkar007 commented Sep 30, 2019 via email

@ganginenivenu
Copy link

ganginenivenu commented Oct 18, 2019

git push new-origin --mirror

When i run command in Git bash, It is giving error you are exceeded the Git hub limit.
Git supports 100 MB while pushing the commits/Repo but my cloned existing Repo size is 350 MB.
How can we achieve this?

Steps executed:

  1. I have Cloned one repository from Gitlab eg: Repo1
  2. cd " Repo1"
  3. git add origin "Repo2"(which is created in github not gitlab)
  4. git push new-origin(Repo2) --mirror

Above mentioned error occurs and i could't push the cloned Repo1 to new Repo2 which is created in git hub.

@ravindrabhandarkar007
Copy link

ravindrabhandarkar007 commented Oct 18, 2019 via email

@ganginenivenu
Copy link

github error

@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 git@github.com: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.

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