How do I zip up a folder but exclude the .git subfolder
Andrew Henderson
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.
8 Answers
The correct expression is -x '*.git*', so the full command should be:
zip -r bitvolution.zip ./bitvolution -x '*.git*'An explanation from :
8The 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
--excludeis 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.svndirectories anywhere in the directory tree.
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 HEADYou 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/ HEADYou can also adjust the compression level between 0 (no compression) and 9 (maximum compression) inclusive, for example
git archive -o bitvolution.zip -9 HEADFor other options, see the help page (git help archive).
I added backslash:
zip -r bitvolution.zip bitvolution -x \*.git\*man page about backslash:
1The backslash avoids the shell filename substitution, so that the name matching is performed by zip at all directory levels.
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 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 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/* Here's an example of what I use:
zip -r workspace.zip workspace -x '*/.git/*' -x '*/.DS_Store/*' -x '*/build/*' 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...