11
git checkout master
git archive stage | tar -czf archive.tar.gz htdocs
# archives master because it's checked out.

How can I archives the stage branch, regardless of the current I'm on?

Ryan Florence
  • 13,361
  • 9
  • 46
  • 63

1 Answers1

19

git archive creates a tar archive so you don't need to pipe its output tar, indeed it's not doing what you expect. You are creating an tar archive of the stage branch and piping it to a normal tar command which doesn't use its standard input but just creates its own archive of htdocs in the working tree, independently of git.

Try:

git archive stage >stage.tar

or, for a compressed archive:

git archive stage | gzip >stage.tar.gz

To just archive the htdocs subfolder you can do:

git archive stage:htdocs | gzip >stage-htdocs.tar.gz

or, to include the folder name in the archive:

git archive --prefix=htdocs/ stage:htdocs | gzip >stage-htdocs.tar.gz

or more simply:

git archive stage htdocs | gzip >stage-htdocs.tar.gz
CB Bailey
  • 755,051
  • 104
  • 632
  • 656
  • That last one will archive the root tree of the commit and prefix every pathname with `htdocs/` (e.g. `htdocs/rootfile.c`, `htdocs/sub/file.pl`, and `htdocs/htdocs/htdocfile.html`). It should either specify the tree-ish `stage:htdocs` (to which the prefix is re-added), or you could just use `git archive stage htdocs`. – Chris Johnsen May 27 '10 at 05:42
  • @Chris Johnsen: Thanks, this was actually a copy & paste error; I meant it to be a variation of the previous command. – CB Bailey May 27 '10 at 06:11