6

How do I exclude files & directories from git diff --no-index?

For example, I want to show the differences between two directories project1 & project2 while ignoring their top-level .git directories (project1/.git & project2/.git) and all their .DS_Store files.

ma11hew28
  • 113,928
  • 113
  • 437
  • 631

1 Answers1

1

This is not exactly what you asked for, but it might give you the results you want: Add project2 as a remote of project1 and just git diff their HEADs.

git remote add other ../project2/.git
git fetch other

# Perform the diff
git diff other/HEAD

If your project2 is not already a Git repo, you can temporarily make it one (hat tip to Josiah Yoder for his comments):

# Set up project2 repo
cd ../project2
git init
git add .

# Add as remote from project1
cd ../project1
git remote add other ../project2/.git
git fetch other

# Perform the diff
git diff other/master

# Clean up
git remote remove other
cd ../project2
rm -r .git
Michael
  • 7,248
  • 5
  • 56
  • 81
  • I now have a circumstance wherein I want to diff between a repo and a non-repo, which this answer does not cover. – Michael Mar 09 '20 at 15:16
  • 1
    Can't you just do a temporary `git init; git add .` in the non-repo and then `rm -r .git` in the non-repo dir when you are done with the diff? – Josiah Yoder Aug 11 '20 at 14:39
  • By the way, the add is done with `git remote add other path/to/project2/.git`, then `git fetch other`, and `git diff other/master`. In the case of a non-repo, I would perform these commands from the non-repo. – Josiah Yoder Aug 11 '20 at 14:42
  • @JosiahYoder These comments are about my comment, yes? That could work, sure. I went a different route and post-proc a `no-index` diff. (I'm doing ~150 `project1`s per run.) – Michael Aug 11 '20 at 14:54
  • These comments are both about your original answer and your comment. I can see why you went a different route. – Josiah Yoder Aug 11 '20 at 15:01
  • @JosiahYoder Good suggestion, though! I'll edit it into the answer. – Michael Aug 11 '20 at 15:03