tar --exclude: Skip Files (Usage & CI Errors)
tar --exclude drops files matching a glob pattern from the archive.
Excluding node_modules, .git, and caches keeps artifacts small. The pitfalls are pattern matching against the stored path and getting the flag order right.
What it does
tar --exclude=PATTERN skips any member whose path matches the shell-style glob PATTERN. The pattern is matched against the archive member name, so it should reflect the path as tar sees it. --exclude-vcs is a handy shortcut that drops .git, .svn, and similar metadata.
Common usage
tar -czf src.tar.gz --exclude='node_modules' --exclude='*.log' .
tar -czf src.tar.gz --exclude-vcs --exclude='dist' .
tar -czf src.tar.gz --exclude-from=.tarignore .
tar -czf src.tar.gz --exclude='./cache/*' .Options
| Flag | What it does |
|---|---|
| --exclude=PATTERN | Skip members matching the glob |
| --exclude-from=FILE | Read exclude patterns from a file |
| --exclude-vcs | Skip .git, .svn, .hg, and similar |
| --exclude-vcs-ignores | Honor .gitignore-style ignore files (GNU) |
| --anchored | Match patterns from the start of the path |
Common errors in CI
On GNU tar, --exclude must appear before the paths it filters, or it is ignored: put --exclude=node_modules ahead of the trailing . Quote patterns so the shell does not expand them first: --exclude='*.log' not --exclude=*.log. If files you expected to skip still appear, your pattern likely does not match the stored prefix (e.g. ./node_modules vs node_modules); add --anchored or adjust the glob.