Imagine you have a repository on GitHub. And you want to play with objects in the Git internal key value store. For example to create a branch for past commits, rearrange branches, etc, etc.

But it can be preferable to have a test repository to do this if you do not want to lost things...

For me it was to adapt the linear development of an application (GRead) to a workflow i discovered at a Git presentation : Git-flow. I'll explain this in the next post on this blog.

The first thing to do for me was to find a way of duplicate my repository on GitHub on which i can work as it was the original one, with same data.

The solution is easy if you know a little of the Git internal, but for me it was not the case (i just did some pull/commit/push by this time)

So thanks to Thomas Mango for giving me the solution :

0. Some variables

  • $myproject is your project name on github and the name of the directory in which the project was cloned.
  • $myotherproject is the name of the duplicate.
  • $meandmyself if your username on GitHub

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