Admittedly, it took me a while (and a couple of attempts) to clone a bunch of git repos from an old server and put them up on a new one. These are the steps I took to copy a working git repository from the old server and put it up on another server complete with history, branches, and tags.
Clone original repo
git clone <original url>:<repo name>;
Set up repo on git server
If your remote is a Gitosis server, make sure that the new project you will be migrating is set up on gitosis.conf. For more information on Gitosis, click here.
... [group project-group] writable = some-other-project, project members = you, other-guy ...
Go to the newly cloned git repo folder
cd <path to cloned repo folder>;
Check which branch you’re on. make sure you’re on the master branch.
git checkout master; git branch -a;
Track all remote branches
There’s more than one way to do this, but let’s follow the bash script from here.
for remote in `git branch -r | grep -v master `; do git checkout --track $remote; done;
Change remote url to new repo url
git remote set-url <'origin' or remote name> <new url>:<project name>;
Push everything to new repo (includes all branches and tags)
git push <--f> --all <'origin' or remote name> <-u>; git push --tags;
- –all will push all the local and/or tracked branches.
- -u will automatically set upstream, so your repo is linked to the remote one automatically. This means that you don’t have to include the remote repo and branch in the commands whenever you push and pull.
- –f means forced, if the project already exists in the git server and you want to overwrite it. ONLY do this if you’re sure it doesn’t affect anyone or anything else.
git clone firstname.lastname@example.org:project; cd project; git checkout master; for remote in `git branch -r | grep -v master `; do git checkout --track $remote; done; git remote set-url origin email@example.com:project; git push --all origin -u; git push --tags;
Migrating A Lot of Repos Made Easier
Mmmhmm…that can be such a pain in the backside. Good thing, you can write some code to alleviate some tediousness in this task.
All you have to do is create an array (or a similar collection) of the repository names you would like to migrate. If you’re using Gitosis, you could perhaps copy paste the writable line after “writable = ” and split them into an array (using an empty space ” ” as the delimiter).
Next, you would have to loop through the array and print out a line that would resemble the command. Something like..
System.out.print("git clone git@" + oldRepoUrl + ":" + repoName + "; ");
Just run your code and you should come up with a long script that you can copy paste to the terminal/command prompt (For this, I suggest you create a new folder and work there). You can either print and run ALL the commands at once or do each of them one by one. I myself printed and ran several commands separately to make sure things were in their proper place before moving on to the next step.
Important: In your code, once you move from repo to repo, don’t forget to add code that will take you to the directory of the next repo.
System.out.print("cd ../" + repoName + "; ");