319

I'm trying to create a zip file from a folder and I'd like to exclude the .git sub-folder from the resulting zip file.

I have gone to the parent folder of the one I want to zip (called bitvolution) and I'm doing:

zip -r bitvolution.zip bitvolution -x ".git"

But it doesn't exclude the .git sub-folder.

I've tried various combinations, -x .git*, -x \.git/*, -x .git/\*, -x \.git/\*. I've also tried using the full path for the exclude argument... but just didn't get there.

1
  • -x ".git*" worked for me.
    – 2540625
    Jun 6, 2020 at 23:27

8 Answers 8

382

The correct expression is -x '*.git*', so the full command should be:

zip -r bitvolution.zip ./bitvolution -x '*.git*'

An explanation from http://selfsolved.com/problems/zip-command-exclude-svn-director:

The correct incantation is

zip -9 -r --exclude=*.svn*  foo.zip [directory-to-compress]

You can also add a
--exclude=*.DS_Store* to exclude the annoying Mac OS X directory display metadata files.

Notice that the expression passed to --exclude is using the entire original relative directory path as the original string to match against. So .svn/* by itself doesn't work; the wildcard character in front ensures that it matches .svn directories anywhere in the directory tree.

8
  • 28
    I had to wrap the glob in quotes or escape the asterisks with backslashes, like zip --exclude '*.git*' -r directory.zip directory or zip --exclude \*.git\* -r directory.zip directory May 29, 2014 at 19:37
  • 7
    If you're worried about losing other files called something.gitwhatever, you can also use --exclude /.git*.
    – Erin Call
    Sep 25, 2014 at 21:54
  • 5
    in ZSH I had to add a backslash: -x \*.git\* Dec 6, 2014 at 20:14
  • 4
    My preferred alternative to backslashes and quotes in zsh: noglob zip -r out.zip someFolder -x *someExcludedFolder*
    – Sébastien
    Oct 19, 2015 at 14:09
  • 7
    Most of these seem that they will ignore your .gitignore files as well. I'd recommend using '*/.git/*' instead, as it is way less permissive, or the git archive approach that others have mentioned.
    – dsz
    Feb 8, 2019 at 4:02
202

If you're trying to zip up a project which is stored in Git, use the git archive command. From within the source directory:

git archive -o bitvolution.zip HEAD

You can use any commit or tag ID instead of HEAD to archive the project at a certain point.

If you want to add a prefix (e.g., a top level folder) to every file:

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

You can also adjust the compression level between 0 (no compression) and 9 (maximum compression) inclusive, for example

git archive -o bitvolution.zip -9 HEAD

For other options, see the help page (git help archive).

10
  • 37
    Not only will this not include the git folder but also anything that is in the gitignore file will be excluded as well. You sir get an upvote.
    – JoeMoe1984
    Oct 21, 2014 at 10:04
  • 2
    Just what I was looking to do!
    – Bjarte
    Oct 21, 2017 at 11:41
  • 1
    I'm late to the party but this absolutely blew me away. Great answer!
    – Tom
    Oct 25, 2017 at 10:14
  • 1
    i wonder why isn't this the best answer
    – nabtron
    Jun 7, 2018 at 1:52
  • 1
    for me this was the greatest TIL today... Jul 8, 2018 at 17:12
35

I added backslash:

zip -r bitvolution.zip bitvolution -x \*.git\*

man page about backslash:

The backslash avoids the shell filename substitution, so that the name matching is performed by zip at all directory levels.

1
  • prepending asterisks with slash forks for me on Ubuntu 14.04 to exclude directories
    – Dimitry K
    Mar 23, 2016 at 17:04
13

Assuming you have git installed on the machine you are doing this, you can also use git itself to create your archive.

git archive --format=zip HEAD -o bitvolution.zip
6

If you are using zsh, command should look like:

zip -r target_name.zip source_dir -x '/*.git/*'

If you use: zip -r target_name.zip source_dir -x /*.git/*. without 'regex', zsh will process before zip run. You will get error message:

zsh: no matches found: /*.git/*
2
  • Thanks! That's what worked for me using Bash on Ubuntu on Windows. Aug 30, 2018 at 11:48
  • I did not understand how this pattern works, but worked for me at zsh.
    – insign
    Feb 25, 2019 at 11:51
6

Use the following format if you want to ignore an entire folder.

For example, to ignore every node_modules folder, in every endpoint:

zip -r API.zip API/* -x */node_modules/*
1
  • 1
    Very good answer!
    – GoingMyWay
    Jan 4, 2023 at 13:40
6

Here's an example of what I use to remove the .git folder and .DS_Store files found on MacOS and build folders:

zip -r workspace.zip workspace -x '*/.git/*' -x '*/.DS_Store' -x '*/build/*'
0

another way is to use find , then exclude with grep -v

find . -type f|grep -v "*.git"|grep -v "node_modules"| zip myFIle.zip -@

looks easier for me, you might grep -v other strings you wan't to avoid. And if you're on windows , use busybox...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .