If I tar a folder that is a git repository, can I do so without including the .git related files? If not, how would I go about doing that via a command?


Have a look first at git help archive. archive is a git command that allows to make archives containing only git tracked files. Probably what you are looking for. One example listed at the end of the man page:

git archive --format=tar --prefix=git-1.4.0/ v1.4.0 | gzip >git-1.4.0.tar.gz
| improve this answer | |
  • 7
    this only archives root git repo. If submodules were used - their directories will be empty. – Stann May 5 '12 at 5:00
  • @Stann: Thanks, from another submodule user – Mark K Cowan Dec 9 '15 at 18:09

Simplest answer: Add --exclude-vcs. This excludes all version control system directories

Personally I use

tar --exclude-vcs -zcvf foo.tar.gz ./FOLDER_NAME

so all you need to do is add the --exclude-vcs at the end of the command.

| improve this answer | |
  • 1
    All known ones, I suppose? ;) – phk Oct 30 '16 at 6:26
  • 3
    All the options need to be together before the arguments on GNU/Linux. – Josh Habdas Aug 30 '19 at 3:28
  • 2
    As stated for GNU/Linux tar --exclude-vcs -zcvf foo.tar.gz ./FOLDER_NAME – Michael Irey Nov 19 '19 at 7:00
  • 4
    It seems OSX Catalina's tar doesn't support that option... – Fran Marzoa Feb 12 at 11:08
  • 2
    @FranMarzoa @Decy --exclude-vcs is only supported by gnu tar. On MacOS you can install it using brew like this brew install gnu-tar. Then you can run gtar --exclude-vcs [...] – Moritz Apr 30 at 12:30

If you want the archive to include the files tracked by git, but not the git repository itself or any generated or otherwise untracked file, then use git archive.

If you specifically want to exclude .git but include everything else, under Linux or FreeBSD or OSX or Cygwin, tar has a simple option to exclude a directory:

tar -c --exclude .git -f - foo | gzip >foo.tgz

With GNU tar (i.e. under Linux or Cygwin), you can shorten this to tar czf foo.tgz --exclude .git foo.

The POSIX way of creating archives is pax.

pax -w -t -s '!.*/\.git$!!' -s '!.*/\.git/.*!!' foo | gzip >foo.tgz
| improve this answer | |
  • Does git compiles on any system where tar is not available? :-) – Stéphane Gimenez Aug 12 '11 at 23:35
  • 3
    @StéphaneGimenez Probably not. But it does compile on systems where tar has no --exclude option, such as OpenBSD (which admittedly duplicates pax's s option, but that's not a common tar command syntax extension). – Gilles 'SO- stop being evil' Aug 12 '11 at 23:38

Thanks to an answer I got on one of my questions I figured out another solution. So for completeness sake, here is a solution making use of find:

find . -path './.git' -prune -o -print |
  tar -czvf ../archive.tgz --no-recursion -T -

And if you want to exclude possible .git folders inside (sub)+folder:

find . -path '*/.git' -prune -o -print |
  tar -czvf ../archive.tgz --no-recursion -T -

Latter can be also achieved through:

find . -type d -name '.git' -prune -o -print |
  tar -czvf ../archive.tgz --no-recursion -T -

Quite handy if you can make use of the other filtering techniques of find, e.g. modification date, permissions, …

(As so often well-formed file names are assumed, otherwise you need to use null bytes as separators which can mean -print0 for find and --null for tar if supported.)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.