Home > Blockchain >  How can I use git diff --no-index to only show incremental missing/changes in dir2 FROM dir1
How can I use git diff --no-index to only show incremental missing/changes in dir2 FROM dir1

Time:09-30

I want to show recursive differences between 2 arbitrary non-git dirs (I know I can do this by passing --no-index); dir1 and dir2 but subject to the following specific note:

  • I only want to see stuff that is different from dir1 (both files and file contents); I don't care about additional stuff in dir2. I want to know stuff from dir1 that is missing in dir2 or stuff from dir1 that is changed in dir2

I am open to using diff instead of git diff or also git difftools (although despite the docs saying you can use any git diff arg with git difftools, I don't think it can be used in the --no-index way [to compare 2 arbitrary dirs]).

CodePudding user response:

The git diff command has numerous options. Start with --name-status first, just to see what it does:

git diff --no-index --name-status <dir1> <dir2>

Now that you've seen its output, take note of the status letters:

  • A means added: a file on the right side (dir2) is new, i.e., does not have a corresponding file on the left.
  • D means deleted: a file on the left side (dir1) has gone missing.
  • M means modified, R means renamed, T means mode-changed (symlink to file or vice versa), and there are a few more possibilities depending on the flags you provide to git diff. See the documentation for a complete list.

Now consider the --diff-filter option. You can tell Git to ignore (not display anything about) files whose status is one or more of these letters, or to show only files whose status is one or more of these letters.

It seems like you want to see things that have status D (gone missing) or M (file exists on both sides but is different). So --diff-filter=DM would be the correct filter.

You may wish to completely disable rename detection (git diff --no-renames) or allow R status files; this depends on your intended usage.

  • Related