Previously, we covered sharing changes, but that included your repository’s entire history. But what if you only need to export changes from a specific commit or just a particular folder? What if you want to create a backup archive of your repository? Fear not, for Git can handle all of these scenarios and more. Thanks to Stack Overflow for providing helpful hints to enhance this post.
If you need a quick backup of your repository, you can use git archive. For instance, to get a zip file containing your repository’s files:
python
git archive HEAD --format=zip > archive.zip
By default, git archive packages repositories in tarballs, so you can easily pipe it to your preferred data compression program:
git archive HEAD | gzip > archive.tar.gz
You can also archive a remote repository using the –remote= option. However, note that this doesn’t work with GitHub remotes, as they encourage you to use the download button instead. But with any other remote, it should work fine. Refer to the manpage if you encounter any issues.
What if you don’t want a compressed version of the files? That’s possible too, thanks to the checkout-index command. Essentially, it copies everything from your index into a different folder. Here’s how you can export your repository:
css
git checkout-index -f -a --prefix=/path/to/folder/
The -f option overwrites files, and the -a option means all files and folders. Just remember the trailing slash on the –prefix option; it’s essential! Omitting it will make the command think you want to prefix every filename with that argument instead.
If you only want to export a specific file or folder (for example, everything in the bin/ folder and the readme), you can use:
css
git checkout-index -f --prefix=/path/to/folder/ bin/* README.textile
Nice! You can also combine this command with find if you want to export all header files, for example. Explore all the capabilities of checkout-index in its manpage. Daniel Schierbeck has encapsulated this process into a script called git-export, which is worth a look if you need to perform this task frequently.