When comparing two directories with diff -r, you may not want to include the .git folder. Since Git’s internal metadata isn’t relevant to code or content differences, excluding .git will give you a cleaner, more meaningful diff. Here’s how to do it depending on your platform.
GNU diff (Linux, WSL, most distributions)
GNU diff provides a built-in way to exclude directories:
|
1 2 |
diff -r -x '.git' dir1 dir2 |
The -x (or --exclude) flag skips any file or directory whose basename matches the given pattern. With this, every .git directory (at any depth) is ignored.
You can repeat -x if you want to exclude multiple items, such as:
|
1 2 |
diff -r -x '.git' -x 'node_modules' dir1 dir2 |
macOS and BSD diff
The version of diff included on macOS and BSD systems lacks the -x/--exclude option. You still have good alternatives:
1. Use Git’s diff outside a repository
Git itself can act as a diff engine, even if you’re not in a repo:
|
1 2 |
git diff --no-index -- ':!**/.git/**' dir1 dir2 |
The --no-index option tells Git to compare directories that aren’t under version control, while :!**/.git/** excludes every .git folder.
2. Use rsync for comparison
rsync in dry-run mode is also a powerful diffing tool:
|
1 2 |
rsync -ani --delete --checksum --exclude='.git' dir1/ dir2/ |
-apreserves attributes-nruns in dry-run mode (no changes made)-iitemizes changes--deletehighlights files only in one side--checksumensures real content comparison, not just timestamps
This will print a list of differences while ignoring .git.
Takeaway
- Linux/WSL (GNU diff):
diff -r -x '.git' dir1 dir2 - macOS/BSD: Use either
git diff --no-indexorrsync -aniwith an exclude pattern.
By excluding .git, your directory comparisons stay focused on meaningful changes — not Git’s housekeeping files.




