So you create a git repository (
repo) on your local machine (
foo) and begin hacking away. Then, you want to push a backup copy of that repo to another box (
bar). What's the best way to do that? There are many ways to do it, but some ways are dangerous and some are just cumbersome.
Ideally, git would have an option to push that instructs it to initialize the remote repository and just do the right thing. I hope this is in our future, but for now we have to do some dancing around.
The canonical way is
bar:~/repo$ git clone --mirror local:repo
foo:~/repo$ git push bar:repo --mirror
but that's not always feasible due to firewalls and nasty NATs.
Unwise methods include copying the working tree with rsync or scp, doing something like the above without
--mirror (which implies
--bare), and other methods that would have you pushing to a non-bare repository.
The best method I've found is this:
bar:~$ git --git-dir=repo init --bare
foo:~/repo$ git remote add --mirror bar bar:repo
foo:~/repo$ git push bar
We set up a bare repository on bar, then set up a "remote" for bar which automatically mirrors (you could do that with the canonical method described above too), so it sends the whole shebang. After that, we push as we normally would.