git ready

learn git one commit at a time
by Nick Quaranto

exporting your repository

committed 29 Jan 2009

Previously there was a tip that covered sharing changes but that included all of your repository’s history. What if you just want to export a certain commit’s changes? Or just one folder? What if you wanted to make an archive of the repository for backup? Fear not, for Git can do all that and more. Thanks to Stack Overflow for providing with some helpful hints to add into this post.

If your need is to just make a quick backup of your repository, doing a git archive will help. So if you wanted to get zip file packed with your repository’s files:

git archive HEAD --format=zip >

The archive command normally packages repos in tarballs, so you can easily pipe it to your favorite data compression program:

git archive HEAD | gzip > archive.tar.gz

You can also archive a remote using the --remote=<repo> option. Just be aware that this does not work with GitHub remotes, as they encourage you to use the download button instead. With any other remote it should work fine though, and check the manpage if you’re having issues.

What if you don’t want a compressed version of the files? That’s possible too thanks to the checkout-index command. Basically, it copies everything on your index into a different folder. Exporting your repo would then be:

git checkout-index -f -a --prefix=/path/to/folder/

The -f option overwrites files, and the -a option means all files and folders. Just don’t forget the trailing slash on the --prefix option, as it’s very important! Omitting it will make the command think you want to prefix every file name with that argument instead.

If you wanted to just export a specific file or folder (in this case everything in the bin/ folder and the readme):

git checkout-index -f --prefix=/path/to/folder/ bin/* README.textile

Nice! You can also chain this command with find if you wanted to export all header files for example. Check out all you can do with checkout-index at its manpage. Daniel Schierbeck has wrapped this process up into a little script called git-export that is worth a look if you need to do this often.