Friday, June 15, 2012

Duplicate a Git[hub] repository

I wanted to use one of my codes as the basis for a new project. Since it was maintained using git, I was looking for a way to copy the code so i can use it as a new repository. This, unlike branching, is a clear duplicate of the original code. 
Thanks to this post, the problem became simple. This a blatant copy-paste, just for my convenience:

1. Create a new repository on GitHub, but don't follow steps after that. Just create it.
You now have https://github.com/$meandmyself/$myotherproject
Duplicate your local copy of the repository :

 cp -a $myproject $myotherproject
 
Change the origin of the copy :
 
 cd $myotherproject
 git remote rm origin
 git remote add origin git@github.com:$meandmyself/$myotherproject.git

And then push your copy online :
 
 git push origin master

It's done !

If you want to copy branch too, note that they are already in your local copy. You just have to do this : (work for each local branch) :
 
 for branch in $(git branch | sed -e s/\*//g); do if  "$branch" != "master" ; then
   git checkout $branch
   git push origin $branch
   git checkout master
   git branch -f $branch origin/$branch
 fi; done

No comments:

Post a Comment