Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore directory on git diff --no-index?

Tags:

git

git-diff

I am trying to create a colorized diff between two folders in the same repository (This may be the totally wrong approach).

I believe one way of achieving this is through git diff --no-index folder1 folder2 and it will automatically create a patch for you (Cool).

However, the diff also includes (as one might expect), all of the .gitignore'd files as well.

folder1
  node_modules
  src/
  .gitignore
  package.json
  README.md

folder2
  node_modules
  src/
  .gitignore
  package.json
  README.md

I would like to ignore node_modules.

Possibilities? Alternatives?

like image 449
chemoish Avatar asked Jan 30 '26 09:01

chemoish


1 Answers

Cheaty version:

git diff $(git commit-tree -m '' @:folder1) $(git commit-tree -m '' @:folder2)

which makes an on-the-fly commit for each tree (as they were on checkout) and diffs the two commits.

You can easily make scratch commits if you want to do this for the current worktree contents,

( export GIT_INDEX_FILE=.git/scratch-index
  git add .
  temp=`git write-tree`
  git diff $(git commit-tree -m '' $temp:folder1) $(git commit-tree -m '' $temp:folder2)
)

and for finer control over what you're using you can pipe git ls-files with whatever pathspecs you want to git update-index --add --stdin.

like image 137
jthill Avatar answered Feb 02 '26 00:02

jthill