What is the best way to create a local backup of a git repository hosted on GitHub, given the following requirements?:
The local backup should be a bare repo.
The backup should include all branches.
It should be easy to (incrementally) update the backup.
Basically, I want a perfect mirror, with the possibility to update easily. As such, the command
git clone --mirror git://github.com/...
comes to mind, but as far as I can tell, that doesn't allow for an easy update (I'd have to delete and recreate my local backup). Also, the mirror option for git clone seems quite recent, I don't have it on some of the systems I'm working on (which have slightly older versions of git running).
What is your recommended solution for this kind of problem?
Not sure what you mean by that, updating it should be as simple as
git clone
as it is is supposed to fetch all refs/commits that are visible on the remote branch.git clone --mirror
is also not very different togit clone --bare
[source]the only relevant difference is the shorthanded
git remote add --mirror
( See
git help add
for the different behaviour )If you're really worried, you can do this:
Which will only do anything different if they were on the same filesystem anyway.
And if you're really paranoid, you can tar.(gz|bz2) the whole directory and back that up.
If you just need to backup GitHub repositories, you can have a look at simple Bash script.
https://github.com/ptrofimov/github-backup-sh
I wrote a ruby script with the help of some others:
https://github.com/walterjwhite/project.configuration/blob/master/scripts/github.com.backup.ruby
That script allows me to download all of my repositories. I use it to periodically make a backup of the projects I am working on.
I hope this helps, feel free to tweak it. I think it has a bug, occasionally, GitHub will timeout and the script doesn't handle that.
To create the mirror:
To update:
To update without changing the current directory:
What are you are asking is quite difficult to do within the constraints of git. The problem is that neither cloning nor fetching will give you all the branches by default. See this question:
For an example of cloning a repo with multiple branches, here is a transcript:
You can see that cloning each branch programmatically is going to be a little tricky.
If github provides access to rsync or Unison these are better tools for the job. Otherwise, you'll have to write some scary scripts...
I am not sure it could cover all your requirements, but you could check out git bundle
What I like about that solution is the single file produced, with exactly what I want in it
Note: Kent Fredric mentions in the comments a subtlety from
git rev-list
:He adds:
To see the difference: