I want to setup a remote repository in my git project so that whenever I push to the ‘real’ remote repository that I cloned from, I also push to a second repo – a (literally) backup repository. Further, I want the ability to push to this backup repository whenever I wish – perhaps on every commit. I tend to commit often, make throw-away branches, etc. I don’t like having much that is not backed up at any point in time (ex. Time Machine on my work Mac)
End goal: I want a backup/off-site, ‘ghost’ repo that I can push to frequently in case I do something horrid to one of the operating systems on my laptop and render unreadable the partition that contains all my work (not that such an occurrence would happen… at least not again this month!).
Step one, for any given project that I want to backup, create a repo that I can access. Step two, add this new ‘remote’ (i.e. NOT on my laptop) repo as a remote in this project. Step three (just for fun), make a ‘meta’ remote repo that will push to both the real remote repo and my new, ghost/backup repo together. Fear of lost code, be gone!
In my setup, the ‘remote’ is about .2 meters from my laptop – an Ubuntu Desktop desktop computer. I picked some place to put my ‘ghost’ repo – let’s say: /var/git/cool_project_ghost.git Then I make it readable (make me the ‘owner’), make it and all previous folder executable if needed (that’s all the chmod a+x stuff below), and initialize it:
sudo mkdir -p /var/git/cool_project_ghost.git sudo chown me:me /var/git/cool_project_ghost.git sudo chmod a+x /var sudo chmod a+x /var/git sudo chmod a+x /var/git/cool_project_ghost.git cd /var/git/cool_project_ghost.git git init --bare
Note: you may need to open your firewall to let port 9418 through with whatever restrictions you like
On my laptop (where the actual coding happens), I change to the root of my project (where .git lives). From here I simply add a remote that I’ll call bup (for back-up). I found it best to be in the master branch before pushing. Then I push everything.
Before this will work, I set-up an SSH user on the Desktop box – in my case, I used a password-less SSH login.** For demo-purposes, my user is “user” and my Ubuntu box will be called “home-box” whose IP address is in /etc/hosts
git remote add bup user@home-box:/var/git/cool_project_ghost.git git push --all bup
At this point, any time I use “git push bup“, the normal git-push actions happen and send data to my “home-box”. Nice, now I can commit and push to my ghost-backup as often as I wish. Whew. This, sadly, makes me feel a lot better.
** Probably foolish since it is a laptop (read: portable!) that I’m using. In my rather particular situation, I will only be pushing when I am at-home with an ‘internal’ IP address – no one will be able to use the passwordless key without also being in my home-network… at which point there are much bigger problems than git-repo access!
Pretty standard stuff so far. In fact, the only reason for me to write what I did above is so that I (and you) might have all of this in one place. Just like thousands of other bloggers, I’m sure. Now comes the fun part. I got what I’m about to post from one elliottcable on this Stack Overflow page. This, upon other searches, comes right from Linus himself.
The solution is super quick-and-easy. Run git config -e and create a new ‘meta’ remote that consists of the ‘real’ remote and my backup/ghost remote.
git config -e
— note: I simply copied the “url = ” lines from the [remote “origin”] and [remote “bup”] sections and use them to create the new remote —added to end of file: [remote "AllRemotes"] url = user@real_remote_origin.com:/awesome_app.git url = user@home-box:/var/git/cool_project_ghost.git
Now, instead of running git push origin I use git push AllRemotes and my code is pushed to multiple (in this case, two) locations. This makes me feel even better.
Now to go backup my partition table…