GIT clone to external drive for backup

Please note that git itself is excellent at copying only the needed changes to a cloned repository.

If you want a copy of your repo to be regularly updated, do this: You create a bare repository as a backup repository, and then repeatedly push all new changes there (no need to delete the old backup).

Ok, let's start by creating your repo

$ cd /tmp
$ mkdir myrepo && cd myrepo
$ touch hi && git add . && git commit -m "bla" 

So, this is your repository. Now we create the clone:

$ cd /tmp
$ mkdir backup && cd backup 
$ git --bare init
Initialized empty Git repository in /tmp/backup/

Now, let's set up your repo for regular backups …

$ cd /tmp/myrepo
$ git remote add backup /tmp/backup
$ git config remote.backup.mirror true

Then copy everything to the backup:

$ git push backup
Counting objects: 3, done.
Writing objects: 100% (3/3), 206 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
To /tmp/backup
 * °new branch§      master -> master

And see if it worked:

$ cd /tmp/backup
$ git log
commit d027b125166ff3a5be2d7f7416893a012f218f82
Author: Niko Schwarz <niko.schwarzàgmail.com>
Date:   Fri Dec 11 12:24:03 2009 +0100

    hi

Tada, you're set. Therefore, all your script needs to do is to issue git push backup. There's exactly no need to repeatedly throw away the old backup.

The alternative is you can have rsync do it all for you:

rsync -av rsync://rsync.samba.org/ftp/unpacked/rsync /dest/dir/

User Offby adds: Since version 1.5.4, "git remote add" takes a "--mirror" option, which saves you both from having to "git config remote.origin.mirror true", and from having to pass --mirror to "git push".


Because git command is little bit weird you have to use call to execute any git commands from a batch file:

rmdir F:\GitClone /s /q
mkdir F:\GitClone
CD /D F:\GitClone\
call GIT CLONE c/GIT/Repo1/