Git export: How to export a Git project

Git export FAQ: How do I export a Git project, like I would do with a "cvs export" or "svn export"?

There is no "git export" command, so instead you use the "git archive" command. By default, "git archive" produces its output in a tar format, so all you have to do is pipe that output into gzip or bzip2 or other.

Git export example

Here's a simple Git export command I just ran. I moved into the root of my Git project directory, then ran this command to create a new file named "latest.tgz":

git archive master | gzip > latest.tgz

Here's how you'd run a Git export command using bzip2:

git archive master | bzip2 > latest.tar.bz2

Git export help

I keep referring to this command as "git export", because that's the way I'm used to thinking about it, but if you'd like more help on this "git archive" command, just use the following Git help command:

git help archive

This git help command gives you very useful information, including:

--format=<fmt>

Format of the resulting archive: tar or zip.

If this option is not given, and the output file is specified, the format is inferred from the filename if possible (e.g. writing to "foo.zip" makes the output to be in the zip format). Otherwise the output format is tar.

This Git help output also includes the following more complicated git archive (git export) examples:

git archive --format=tar --prefix=junk/ HEAD | (cd /var/tmp/ && tar xf -)
Create a tar archive that contains the contents of the latest commit on 
the current branch, and extract it in the /var/tmp/junk directory.

git archive --format=tar --prefix=git-1.4.0/ v1.4.0 | gzip >git-1.4.0.tar.gz
Create a compressed tarball for v1.4.0 release.

git archive --format=tar --prefix=git-1.4.0/ v1.4.0^{tree} | gzip >git-1.4.0.tar.gz
Create a compressed tarball for v1.4.0 release, but without a global 
extended pax header.

git archive --format=zip --prefix=git-docs/ HEAD:Documentation/ > git-1.4.0-docs.zip
Put everything in the current head's Documentation/ directory into 
git-1.4.0-docs.zip, with the prefix git-docs/.

git archive -o latest.zip HEAD
Create a Zip archive that contains the contents of the latest commit 
on the current branch. Note that the output format is inferred by 
the extension of the output file.

Git export notes

As one word of caution, don't forget the role your .gitignore file plays in this process. I just ran the first Git export command shown above, and wondered what happened to some of my project directories. Turns out I had them set to be ignored in my .gitignore file.

Git export - Summary

I hope these Git export (git archive) examples have been helpful. As mentioned, I just used the first git export example shown above, and it worked just fine.