How to migrate GIT repository from one server to a new one

Git

Git Problem Overview


I have a server that I'm taking down. The only thing I have left to migrate is my repository. This server is listed as the origin (master) for one of my projects. What is the proper way to move the repository to keep the history.

Git Solutions


Solution 1 - Git

To add the new repo location,

git remote add new_repo_name new_repo_url

Then push the content to the new location

git push new_repo_name master

Finally remove the old one

git remote rm origin

After that you can do what bdonlan said and edit the.git/config file to change the new_repo_name to origin. If you don't remove the origin (original remote repository), you can simply just push changes to the new repo with

git push new_repo_name master

Solution 2 - Git

If you want to migrate all branches and tags you should use the following commands:

git clone --mirror [oldUrl]

to clone the old repo with all branches

cd the_repo
git remote add remoteName newRepoUrl

to setup a new remote

git push -f --tags remoteName refs/heads/*:refs/heads/*

to push all refs under refs/heads (which is probably what you want)

Solution 3 - Git

Updated to use git push --mirror origin instead of git push -f origin as suggested in the comments.


This worked for me flawlessly.

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

I have to mention though that this creates a mirror of your current repo and then pushes that to the new location. Therefore, this can take some time for large repos or slow connections.

Solution 4 - Git

Copy it over. It's really that simple. :)

On the client side, just edit .git/config in the client's local repo to point your remotes to the new URL as necessary.

Solution 5 - Git

This is sort of done in parts in some of the other answers.

git clone --mirror git@oldserver:oldproject.git
cd oldproject.git
git remote add new git@newserver:newproject.git
git push --mirror new

Solution 6 - Git

I'm just reposting what others have said, in a simple to follow list of instructions.

  1. Move the repository: Simply login to the new server, cd to the parent directory where you now want to hold the repository, and use rsync to copy from the old server:

     new.server> rsync -a -v -e ssh user@old.server.com:path/to/repository.git .
    
  2. Make clients point to the new repository: Now on each client using the repository, just remove the pointer to the old origin, and add one to the new one.

     client> git remote rm origin
     client> git remote add origin [email protected]:path/to/repository.git
    

Solution 7 - Git

Take a look at this recipe on GitHub: https://help.github.com/articles/importing-an-external-git-repository

I tried a number of methods before discovering git push --mirror.

Worked like a charm!

Solution 8 - Git

I followed the instructions on BitBucket to move a repo with all its branches there. Here come the steps with explanations following the # character:

cd path/to/local/repo
git remote remove origin # to get rid of the old setting, this was not in the BitBucket instructions
git remote add origin ssh://gi[email protected]/<username>/<newrepo> # modify URL as needed
git push -u origin --all # pushes _ALL_ branches in one go
git push -u origin --tags # pushes _ALL_ tags in one go

Worked nicely for me.

Solution 9 - Git

Please follow the steps:

  • git remote add new-origin
  • git push --all new-origin
  • git push --tags new-origin
  • git remote rm origin
  • git remote rename new-origin origin

Solution 10 - Git

This is a variation on this answer, currently suggested by gitlab to "migrate" a git repository from one server to another.

  1. Let us assume that your old project is called existing_repo, stored in a existing_repo folder.

  2. Create a repo on your new server. We will assume that the url of that new project is git@newserver:newproject.git

  3. Open a command-line interface, and enter the following:

     cd existing_repo
     git remote rename origin old-origin
     git remote add origin git@newserver:newproject.git
     git push -u origin --all
     git push -u origin --tags
    

The benefits of this approach is that you do not delete the branch that corresponds to your old server.

Solution 11 - Git

Should be as simple as:

git remote set-url origin git://new.url.here

This way you keep the name origin for your new repo - then push to the new repo the old one as detailed in the other answers. Supposing you work alone and you have a local repo you want to mirror with all your cruft in it, you might as well (from inside your local repo)

git push origin --mirror # origin points to your new repo

but see https://stackoverflow.com/questions/3333102/is-git-push-mirror-sufficient-for-backing-up-my-repository (in all don't use --mirror but once).

Solution 12 - Git

You can use the following command :

git remote set-url --push origin new_repo_url

Example from http://gitref.org/remotes/

$ git remote -v
github	[email protected]:schacon/hw.git (fetch)
github	[email protected]:schacon/hw.git (push)
origin	git://github.com/github/git-reference.git (fetch)
origin	git://github.com/github/git-reference.git (push)
$ git remote set-url --push origin git://github.com/pjhyett/hw.git
$ git remote -v
github	[email protected]:schacon/hw.git (fetch)
github	[email protected]:schacon/hw.git (push)
origin	git://github.com/github/git-reference.git (fetch)
origin	git://github.com/pjhyett/hw.git (push)

Solution 13 - Git

follow these instructions If you want to keep all the commits and branches from old to new repo

git clone --bare <old-repo-url>
cd <old-repo-directory>
git push --mirror <new-repo-url>

Solution 14 - Git

You can use git-copy to duplicate the repo with all histories.

git copy http://a.com/old.git http://a.com/new.git

Solution 15 - Git

If you want to move from one origin to another and also keep a backup of your current origin on your local machine you could use these steps:

  1. First locally go to the (git)folder you want to move over
  2. Create the new repository online This step creates a repository where we can push code to

Now in the folder do

git remote get-url origin

The above command gives the current remote origin url, useful to set the origin back to in the last step

git remote set-url origin git@github.newlocation:folder/newrepo.git

The above command sets the remote origin to the new location

git push --set-upstream origin develop

The above command pushes the current active local branch to remote with branchname develop. Of course it preserves all history as with git all history is also pushed.

git remote set-url origin <original old origin>

The above command sets back the remote origin to your current origin: you want this because you are in your existing folder and you probably do not want to mix up your current local folder name with the new folder you are going to create for cloning the repo you just pushed to.

Hope this helps,

Solution 16 - Git

If you want to migrate a #git repository from one server to a new one you can do it like this:

git clone OLD_REPOSITORY_PATH
cd OLD_REPOSITORY_DIR
git remote add NEW_REPOSITORY_ALIAS  NEW_REPOSITORY_PATH
#check out all remote branches 
for remote in `git branch -r | grep -v master `; do git checkout --track $remote ; done
git push --mirror NEW_REPOSITORY_PATH
git push NEW_REPOSITORY_ALIAS --tags

All remote branches and tags from the old repository will be copied to the new repository.

Running this command alone:

git push NEW_REPOSITORY_ALIAS

would only copy a master branch (only tracking branches) to the new repository.

Solution 17 - Git

Remark:

git copy http://a.com/old.git http://a.com/new.git

works only for e.g. a github to github copy, i.e. remaining on the same git system. When copying from e.g. github to gerrit, this does not work.

Besides that, it's quite comfortable, as it copies branches, tags, and submodules automatically.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncjiboView Question on Stackoverflow
Solution 1 - GitKobyView Answer on Stackoverflow
Solution 2 - GitjzwienerView Answer on Stackoverflow
Solution 3 - GitRobertoView Answer on Stackoverflow
Solution 4 - GitbdonlanView Answer on Stackoverflow
Solution 5 - GitIan WaltersView Answer on Stackoverflow
Solution 6 - GitJuan A. NavarroView Answer on Stackoverflow
Solution 7 - GitJohn SmithView Answer on Stackoverflow
Solution 8 - GitLaryx DeciduaView Answer on Stackoverflow
Solution 9 - GitBoushView Answer on Stackoverflow
Solution 10 - GitClémentView Answer on Stackoverflow
Solution 11 - GitMr_and_Mrs_DView Answer on Stackoverflow
Solution 12 - GitmrroboaatView Answer on Stackoverflow
Solution 13 - GitExtreme View Answer on Stackoverflow
Solution 14 - GitQuanlongView Answer on Stackoverflow
Solution 15 - GitHaceView Answer on Stackoverflow
Solution 16 - Gitmaestr0View Answer on Stackoverflow
Solution 17 - GitpatmanView Answer on Stackoverflow