14

I can backup my local .git by pushing it to a repository in two steps:

git push --all ~/gitrepo
git push --tags ~/gitrepo

I can back it up using git bundle.

I can back it up by simply copying the entire directory or archiving it (compressed!):

tar -zcvf gitrepo.tgz .git

And there are probably additional ways to backup an entire local .git.

The question now is whether they are really equivalent? (for example, the logs subdirectory isn't pushed)

What are the advantages of the git push method vs. git bundle?

Can tar -zcvf be considered "the perfect git backup"?

Community
  • 1
  • 1
WinWin
  • 6,745
  • 8
  • 41
  • 52

3 Answers3

8

I use Git bundle

git bundle create /tmp/backup.git --all --tags --remotes

You can receive it as if it were a repo:

cd myworktree
git pull /tmp/backup.git

But also see


Completeness, notes

For complete backup (thing of git-rerere cache, stashes, hooks, configuration files) I suggest using rsync

rsync -hxPavilyzH --stats --delete .git/ backup@remote:/repo/mirror.git/

Alternatively:

Community
  • 1
  • 1
sehe
  • 328,274
  • 43
  • 416
  • 565
5

Regarding tar: It saves everything: the config (remote URLs), the reflogs, etc. You might want the reflogs if you accidentally do something really stupid in your repository and your hard drive crashes shortly afterwards. Far-fetched, but tar is easy and it does everything. Use tar for making backups, use git push for making mirrors. These are different kinds of tasks.

Regarding compression: I have a 27M git repo, almost entirely plain text, and the .tar.gz is... also 27M. That's not a whole lot of savings.

Dietrich Epp
  • 182,361
  • 34
  • 307
  • 387
  • 1
    All answers were excellent: @VonC introduced me to `--mirror` and `stash`. @sehe clarified `bundle` and you pointed out the difference between a *backup* and a *mirror* (plus the interesting fact that a git repo is already in compressed form?). SO only allows accepting 1 answer and you have the least number of points, so I am accepting yours. :) – WinWin Jul 26 '11 at 17:29
  • I used tar for git backup, but now i wanted to migrate some repos on other server and untared repos are giving me strange errors. Did any of you got this errors? – Nemke May 11 '13 at 10:17
  • If anybody gets strange errors like error: refs/remotes/origin/master does not point to a valid object! error: Trying to write ref refs/heads/master with nonexistant object after untaring repo and while trying to git pull, you should check you git version. Apparently, you need git with at least 1.7.11 version and that will solve you problems. – Nemke May 11 '13 at 10:35
  • FYI: If you `tar -zcvf gitrepo.tgz .git` you will not backup any untracked files -- i.e., files that have not been added to the repo – JESii Jun 25 '18 at 13:28
5

The tar method is a possibility, but it won't check the integrity of the saved repo: you won't know if that compressed repo would work until you uncompress it and try to clone or fetch from it.

I prefer the clone --mirror approach (with reflogs enabled in the resulting bare repo).
And the incremental backups are then simple push.
As debated in this old thread, a git stash before a backup might allow you to save more (index and working tree state)

Community
  • 1
  • 1
VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283