Home·Terminal commands

git archive

git

Usage

git archive --format=tar.gz -o <file> <tree-ish>

Exports the files of one commit as a tar or zip; .git and untracked files are left out, which makes a cleaner release archive than zipping the folder.

Common flags

FlagMeaning
--format=<fmt>tar, zip or tar.gz
-o <file>write to a file; the format is guessed from the name
--prefix=<dir>/put everything under one folder inside the archive
--add-file=<file>include an untracked file as well
--remote=<repo>let the server build it
-llist the formats this build supports

Examples

git archive --format=tar.gz -o app-1.2.0.tar.gz v1.2.0

a release tarball of exactly that tag

git archive --prefix=app/ -o app.zip HEAD

a zip whose contents sit inside app/

git archive -o src.zip HEAD -- src

only the src directory

The confusion is that undo has several shapes — moving where a branch points is not the same as adding a commit that reverses it.

How to read this

  • Square brackets [ ] mark a part you may leave out.
  • An ellipsis … means you can list more than one.
  • Flags are case-sensitive — in some commands -r and -R do different things.

Questions

Q. What does git archive do?

Exports the files of one commit as a tar or zip; .git and untracked files are left out, which makes a cleaner release archive than zipping the folder.

Q. How do I type it?

git archive --format=tar.gz -o <file> <tree-ish> — square brackets mark the parts you can leave out.

Q. How many flags are worth knowing?

6 are listed here; the full set is in man git. This command sits under git.

Related commands

man page: man git-archive