December 4, 2014

Git Beyond the Basics: Bundle and Archive

Git at Scale

This is part 4 of a 6-part series on Git commands.

git beyond the basics

Git’s bundle and archive commands aren’t broadly known but can serve a variety of purposes. Both provide the means to output portions of a repository, the key distinction being what they include:

  1. Archive sends only the specified file contents to the standard output.
  2. Bundle creates a single file containing both file contents and history.

In other words, they differ as to whether the contents of the .git folder are included: archive excludes it while bundle includes it. Practically, the two commands serve a variety of purposes, such as backing up just the files in a single source tree (e.g., the head revision), backing up the entire repository, transferring a branch to secure machines that have no network connections, etc. For example:

git archive --prefix=2014-10-21/ --format=zip HEAD >HeadArchive.zip

The above command will archive all the contents of the current HEAD into a file named HeadArchive.zip. And because the prefix option is given, it will do so under a folder named “2014-10-21” in the archive. The available list of formats can be retrieved with the git archive --list command. Many other options are also available, as explained by the Git Book documentation.

But of course that includes only the files; it doesn’t include their history. For that you want the bundle command instead. Consider the following example:

git bundle create HeadBundle.git HEAD

That command will create a single file named HeadBundle.git that contains not only the entire file contents of the current HEAD but all the history and versioning metadata as well. As such, a bundle serves as a complete copy of the specified data. You can even clone a new repository from it:

git clone HeadBundle.git ~/test

That command will create a clone of the contents of the bundle in a new test folder. Other options let you verify the contents of a bundle, list the contents, and so forth. By establishing a clear convention for tagging, the bundle command even makes it possible to transfer incremental changes regularly at a given time interval. For more information, consult the Git Book documentation.

Keep reading

Looking for more information on Git commands? Here are other Git Beyond the Basics posts that you might find helpful: