Backing up a git repository with git bundle

I have a git repository called ~/devel/ which is where all my development effort in various languages is stored (you can see that it’s not that much, otherwise I’d have separate repositories ;). I also occasionally merge projects that started in their own repositories into this one.
Anyway, while backing ~/devel/ to my new USB-drive i noticed that 1. the FAT32 partition seemed to mess with git, as git status showed _all_ of the files as changed and 2. i also backed up files which were in ~/devel/ but not in the repository (which is not what I want). I could have done something like git-archive or just backed it up without .git, but I want to keep the history.
In this case git bundle is what you want. git bundle was designed to allow transfer of git commits when there is no direct connection between repositories (i.e.: offline) but using patches is not an option because of the large number of commits and multiple branches. A git bundle is just one file which can be very easily created and again imported as it can be treated like another remote. A quick example:
jojo@dualtron:~/devel$ git bundle create ~/devel.bdl master test and a bundle is saved under ~/devel.bdl containing my master and test branches. If I am now at repository B I just use jojo@dualtron:~$ git ls-remote devel.bdl which shows me the branches stored in the bundle. To use the bundle I simple treat it like a remote, using git fetch (for example) jojo@dualtron:~/git/repoB$ git fetch ~/devel.bdl refs/heads/\*:refs/remotes/bundle/\*
If all of this doesn’t make any sense, git help bundle is your friend, as usual. If you still not get it afterwards you haven’t really understood how git works.
P.S.: If there is an easier way to accomplish this, or if there are any factual errors, please leave a comment!

5 Responses to “Backing up a git repository with git bundle”

  1. Wouldn’t a quick tarball of the .git directory suffice?
    tar -czvf backup.tgz devel/.git

    git bundle has its uses of course, but this seems a bit overkill.

  2. Can’t you use git clone bundle.name head?

  3. Yeah, sure, if you’ve got no initial repo you can just use clone as well.

  4. The tarball solution doesn’t offer you incremental backups (unless you use tar’s incremental backup facility — but that is overkill IMHO), and it doesn’t filter out branches that you wouldn’t be interested in backing up. git bundle is a nice and clean way of using the git tools to “backup” your repo the git way.

  5. Hi I used this a while last year to backup unused Git repositories. One of those repositories had to be restored and was so simple to do so, restored to the exact state it was left in.

    Awesome Thanks