46

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?

Michael Mrozek
  • 91,316
  • 38
  • 238
  • 232
chrisjlee
  • 8,283
  • 16
  • 49
  • 54

5 Answers5

85

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.

AdminBee
  • 21,637
  • 21
  • 47
  • 71
mtelesha
  • 1,121
  • 1
  • 7
  • 7
  • 2
    All *known* ones, I suppose? ;) – phk Oct 30 '16 at 06:26
  • 3
    All the options need to be together before the arguments on GNU/Linux. – vhs Aug 30 '19 at 03:28
  • 3
    As stated for GNU/Linux ```tar --exclude-vcs -zcvf foo.tar.gz ./FOLDER_NAME``` – Michael Nov 19 '19 at 07:00
  • 7
    It seems OSX Catalina's tar doesn't support that option... – Fran Marzoa Feb 12 '20 at 11:08
  • Getting a `tar: Option --exclude-vcs is not supported` in macOS – Devy Apr 16 '20 at 20:51
  • 7
    @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 '20 at 12:30
  • Just to add a little more info on which VCS's are supported, [GNU tar :: 6.4 Excluding Some Files](https://www.gnu.org/software/tar/manual/html_node/exclude.html) states the following: `Exclude files and directories used by following version control systems: ‘CVS’, ‘RCS’, ‘SCCS’, ‘SVN’, ‘Arch’, ‘Bazaar’, ‘Mercurial’, and ‘Darcs’.` (.git included) See more in the doc. – ATutorMe Dec 03 '21 at 07:37
41

Have a look at git help archive or git archive --help. The git subcommand archive allows you to make archives containing only files trackeod by git. This is probably what you are looking for.

One of many examples listed at the end of the manual:

git archive --format=tar.gz --prefix=git-1.4.0/ v1.4.0 >git-1.4.0.tar.gz

A current version of git supports creating archives in the following formats:

  • tar
  • tar.gz or tgz
  • zip

See git archive --list for a list of formats supported on your system.

Kusalananda
  • 320,670
  • 36
  • 633
  • 936
Stéphane Gimenez
  • 28,527
  • 3
  • 76
  • 87
  • 10
    this only archives root git repo. If submodules were used - their directories will be empty. – Stann May 05 '12 at 05:00
27

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
Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
0

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.)

phk
  • 5,893
  • 7
  • 41
  • 70
0

The only thing that worked for me to exclude both the .git directory (and another called backups) to transfer to another server just the necessary files, it's this. Gilles answer helped me find this solution.

tar --exclude .git --exclude backups -czvf project1.tar.gz /var/www/html/project1/
cdsaenz
  • 101
  • 1