How to migrate GIT repository from one server to a new one
Asked Answered
D

18

313

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.

Dress answered 27/9, 2009 at 22:26 Comment(3)
All the information (orgin, trunk, etc.) about the repository are stored in a folder named '.git', where you are initializing the repository. So, you need to copy the contents to the new server.Sacrum
simply git config remote.origin.url newurl see #3011902Infective
If you wish to migrate your repo and all the branches use the answer by jzwiener or Roberto rather than the accepted answer.Hedgehog
E
263

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
Eberly answered 27/9, 2009 at 22:37 Comment(17)
Would this only copy a single branch from the old repository to the new one?Irritant
This would yes, but since you, you're just changing the remote, you can push each branch to the new remote.Eberly
What is the solution to migrate all branches?Erl
@Erl I would look at something like what's done in this SO answer, and loop through all your branches and push them.Eberly
You should be able to push all branches at once via git push -u new_repo_name --all.Ghats
Migration of all branches can be found at https://mcmap.net/q/89509/-how-to-migrate-git-repository-from-one-server-to-a-new-oneEffluence
i prefer the "copy over"/rsync solution as it does not lose e.g. hooks and other configuration. this version does.Palatable
N00b question, but does this also copy the commit history?Romans
git push -u new_repo_name --all does NOT work. I just tried it and it only commits the master branch!Motorcycle
This answer is misleading. The question does not specify that they only wanted master. A user may be under the false impression that this will indeed move the entire repo when it won't.Aenneea
Instead of editing the git-config file, this command worked for me: git remote rename new_repo_name originTablecloth
Note: git remote rm does not delete the remote repository from the server. It simply removes the remote and its references from your local repository.Forethoughtful
if you face error fatal: remote origin already exists. , you should do a git remote set-url origin git://new.url.here instead of addClyde
Before following your answer, I had to do this on the server where I wanted the repo to go: git init --bare new_repo_name.git. Then I followed your instructions and it worked perfectly. Without that, when I tried to push I got fatal: 'new_repo_name.git' does not appear to be a git repository fatal: Could not read from remote repository. You might want to suggest this in your answer.Derekderelict
According to @Eberly answer and @Ghats comment, the shortened version: git remote rm origin git remote add origin new_repo_url git push -u origin --all Note, as @the-new-mr said, this solution works only for local branches.Winebaum
looks like it can miss branches. This answer is simple and comprehensive: https://mcmap.net/q/89509/-how-to-migrate-git-repository-from-one-server-to-a-new-oneMistral
This doesn't answer the question; it only migrates a single branch. Following this advice will cause data loss.Satellite
S
261

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.

See also the receipt in section "Extra" of How to move a git repository with history | Atlassian Git Tutorial.

Shipway answered 24/10, 2014 at 17:0 Comment(4)
This is the good solution git remote set-url origin <URL to my NEW repo location> (after having rsync'ed the old origin to the new server/location)Cartilaginous
Prefer git push --mirror origin over -f.Quill
Git version 2.17.1 using git push -f origin pushes only the working directory's current branch. @Quill 's recommendation for git push --mirror origin worked for me, pushed all branches, tags, history, etc.Cowcatcher
There are 2 steps missing, that have to be done first: 1) create a bare remote: sudo git init --bare /path_in_remote/my_repo.git 2) make it writable: sudo chown -R www-data /path_in_remoteMispleading
E
229

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)

Effluence answered 20/8, 2013 at 13:19 Comment(6)
Git was complaining that --tags and refs/heads/*:refs/heads/* are not compatible with --mirror. https://mcmap.net/q/89509/-how-to-migrate-git-repository-from-one-server-to-a-new-one worked well.Hernandes
I used this and it worked for me. Probably should be the elected answer. Note that the "code" you get locally appears to be heavy on the meta data which makes it unclear for newbies if something went wrong.Ethelstan
This should be the approved answer. Much better than other solutionsVhf
I ended up with all the branches on my new remote repository prefixed refs/heads/refs/heads using git push -f --tags remoteName refs/heads/*:refs/heads/* so I have swapped to git push remoteName --mirrorBohs
push also supports --mirror. git clone --mirror; cd repo; git push --mirror new_remote should do the trickGluten
too complicated, seems that some have indicated problems. This answer is simpler and problem-free: https://mcmap.net/q/89509/-how-to-migrate-git-repository-from-one-server-to-a-new-oneMistral
H
82

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.

Heddle answered 27/9, 2009 at 22:28 Comment(4)
You can also simply clone it. Also, instead of directly editing the .git/config file, you can use git remote rm origin; git remote add origin <new repository>.Reg
rming the remote will lose any configuration under that section of the config - and cloning it without taking any extra steps will lose branches other than trunk. It's possible to deal with these problems, but, really - just rsync it.Heddle
What is the solution using only git to do the work? rsync requires additional administrative hoops that are difficult to jump throughErl
this also preserves e.g. hooks and other configuration, so i prefer it to pure git solutionsPalatable
D
78

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
Defilade answered 5/5, 2016 at 4:50 Comment(5)
This is, in fact, the most complete, straight-forward answer.Democratize
--mirror in the push is much important: this should be the correct answerSalaidh
When you initialize the new repository on the new server remember to do a bare init or the push will not be successful: git init --bareJameljamerson
This is the right way, bare repositories pull down the files and all histories across branches. This migrates the entire working history: effectively a like-for-like copy.Papilionaceous
I have followed the same steps but facing an error: failed to push some refs to "git@newserver:newproject.git"Tenpins
H
37

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 [email protected]: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
    
Harlandharle answered 3/5, 2013 at 14:10 Comment(3)
Simple and effective. You can add the flag --bwlimit=XXX if you want to limit the traffic between the servers, where XXX equals the bandwidth in KBytes per second.Astute
Bit better than remove and add: git remote set-url origin [email protected]:path/to/repository.gitSportsmanship
For those deploying to a server using git+capistrano, note that I had to use set-url origin in 2 places: on localhost and on the cached-copy that's on the server.Housley
R
16

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!

Rosenbaum answered 15/2, 2014 at 0:27 Comment(1)
So basically git clone --mirror ..., git remote add ..., git push --mirror ...Marquet
S
10

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://[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.

Sexpot answered 23/1, 2014 at 9:44 Comment(0)
I
10

Please follow the steps:

git remote add new-origin <GIT URL>
git push --all new-origin
git push --tags new-origin
git remote rm origin
git remote rename new-origin origin
Igniter answered 27/5, 2014 at 2:17 Comment(0)
S
7

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.

Selfdevotion answered 25/1, 2019 at 16:20 Comment(0)
S
6

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 Is "git push --mirror" sufficient for backing up my repository? (in all don't use --mirror but once).

Slowpoke answered 3/6, 2014 at 12:11 Comment(0)
S
5

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>
Solberg answered 2/5, 2017 at 18:25 Comment(0)
E
4

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)
Effeminate answered 19/5, 2013 at 15:15 Comment(0)
E
2

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

git copy http://a.com/old.git http://a.com/new.git
Episcopalism answered 26/3, 2015 at 15:43 Comment(0)
R
1

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 [email protected]: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,

Ratiocinate answered 20/2, 2018 at 13:34 Comment(0)
D
0

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.

Disarrange answered 18/10, 2013 at 11:18 Comment(0)
L
0

I have this bash script written that works as a breeze to migrate my git repo from one server ( one vender , repo hosted in bitbucket ) to another server ( repo hosted in gitlab or local git hosting ) with this command like tool , I only have to authenticate to the new git server where I want to migrate the code from the old repo server.

    #!/bin/bash

# Default values
source_server=""
destination_server=""
source_repository=""
destination_repository=""


# Parse command-line options
OPTS=$(getopt -o "s:d:S:D:" --long source:,destination:,source_repository:,destination_repository: -n 'git-migration' -- "$@")
if [ $? != 0 ]; then
    echo "Failed to parse command-line options. Exiting..."
    exit 1
fi

eval set -- "$OPTS"

# Handle command-line options
while true; do
    case "$1" in
        -s | --source)
            shift
            source_server="$1"
            shift
            ;;
        -d | --destination)
            shift
            destination_server="$1"
            shift
            ;;
        -S | --source_repository)
            shift
            source_repository_link="$1"
            shift
            ;;
        -D | --destination_repository)
            shift
            destination_repository_link="$1"
            shift
            ;;
        --)
            shift
            break
            ;;
    esac
done

# Check if required options are provided
if [[ -z "$source_server" || -z "$destination_server" || -z "$source_repository_link" || -z "$destination_repository_link" ]]; then
    echo "Missing required options. Please provide source server, destination server, and source_repository and destination_repository. Exiting..."
    echo "syntax : migrationtool --source <source_foldername> --destination <destination_foldername> --source_repository <source_Repo_link> --destination_repository <destination_repo_link>"
    echo "Any one option missing will fail the program in the initial phase itself with returncode 121"
    exit 121
fi

#clone the repo first 
git clone $source_repository_link
#list all the branches that are present in the current old repository 
cd $source_server
git branch -a 

# save the list of the branch names into a txt file for further iteration 
git branch -a  | sed s/'remotes\/origin\/'// | grep -v HEAD | grep -v "*" > ../git_branches.txt

# Now we have to checkout each branch to load the data on the folder, in order to see the branch name 
# when we give the command "git branch" this loads only the branches that are checkout mostly
# for this reason this next iteration step is useful
for i in `cat ../git_branches.txt `;do git checkout $i ;done

# fetch all the tags in the old repository 
git fetch --tags

# this shows all the branch and tags that we have fetched for us. 
echo "**********All the tags**********"
git tag
echo "**********All the branches ***********"
git branch -a 

# now if your New repository on the other vendor like github is going to be different then you have to 
# rename the folder name to sync up with repository name that you have created in the github
# mostly the name of the repository is going to be same but here I can use the passing of variable
if [[ -z $destination_server ]];
then 
    cd ..
    mv $source_server $destination_server
    cd $destination_server
fi

# remove the origin from this folder, here only the directory name is matched wrt to the new repository
# but the link to the old repo still exists that we are going to break 
git remote rm origin 
git remote add origin $destination_repository_link

#check the output of the new vender git link 
echo "******* new github vendor link for the project*******"
git remote -v

echo ""
echo ""
# once the origin of the new Git repo is added successfully, then we can push the changes to the new 
# repository vendor 
git push origin --all
git push --tags


## cleanup should be there
cd ..
rm -rf $source_server
rm -rf $destination_server
rm -rf git_branches.txt

Usage : cp script.sh /usr/local/migrationtool

migrationtool  --source old_Git_Repo --source_repository https://gitlab.com/developer123/old_Git_Repo.git
--destination_repository https://github.com/developer123/new_git_Repo.git --destination new_git_Repo

Order of the parameters passed is not required to be correct , just have to make sure that the correct arguments are passed with the parameters.

Levitan answered 21/6, 2023 at 17:22 Comment(0)
B
-1

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.

Benzvi answered 24/3, 2022 at 13:32 Comment(2)
git: 'copy' is not a git command. See 'git --help'.Brana
You can extend git, see github.com/cybertk/git-copy.Benzvi

© 2022 - 2024 — McMap. All rights reserved.