Files modified between commits but with a limited list of files for older commands

Situation: I have an old commit, which I need to combine selectively with the last commit. Some files have no changes, while other files have significant changes, which I need to view and group selectively.

Let's say that the old commit 1 had files A , B , C

The last commit 5 is associated with the change of files B and C from the moment of commit 1 , as well as the added files D , E and F

Thus, between commits 1 and 5 , the B and C files were changed, that is, running diff on 1:B and 5:B ; and on 1:C and 5:C differences will be displayed.

I need to get only the file names B and C

That is, all files that do not belong to 1 , but changed or added before and included 5 should display NOT .

+4
source share
4 answers

You can try

 git diff --diff-filter=M 1 5 

Available Filters

  • A : added
  • C : copied
  • D : Deleted
  • M : Modified
  • R : renamed
  • T : type changed (symbolic link, regular file, etc.)
  • U : not working
  • X : Unknown
  • B : articulation broken

See the git-diff(1) page for more information.

Edit

If you are interested in what has changed between the two commits, you can also use the --name-status parameter, which for each file that has changed produces one of the above codes. This option can also be used with git-log to tell you what type of changes were made to the files for each commit.

+2
source

To view only files present in commit 1 , run

 $ git ls-tree --name-only commit1 | xargs git diff commit1 commit5 -- 

Note that this will include files that were present in commit 1 but deleted in commit 5 (which will be displayed as deleted files in diff). If you want to avoid this, find a common subset of files:

 $ git ls-tree --name-only commit1 > all-files-in-commit1 $ git ls-tree --name-only commit5 > all-files-in-commit5 $ comm -1 -2 all-files-in-commit1 all-files-in-commit5 > common-files $ xargs git diff commit1 commit5 -- < common-files 
+1
source

Try it (using SHA 1 and 5 , as in your example):

 git diff 1 5 $(git diff --raw --no-commit-id --name-only -r 1~1 1) 

Or more comfortable, so you only need to dial each SHA once:

 c1="1"; c2="5" git diff $c1 $c2 $(git diff --raw --no-commit-id --name-only -r $c1~1 $c1) 

How it works: The first git diff compares two commits. The second generates a list of paths to limit comparisons. This list is created by comparing files that were changed between your old commit $c1 and its parent $c1~1 . (This also works when specifying something like the tenth last commit using c1="HEAD~10" , since HEAD~10~1 is a valid tree in git, equivalent to HEAD~11 )

The subshell uses git diff --raw to work for commits. Otherwise, we could use git diff-tree --no-commit-id --name-only -r $c1 , which automatically compares the commit with its parent.

TODO: Not yet working. This works from time to time, sometimes I get a "fatal: ambiguous argument" <file_name> ': unknown revision or path not in the working tree. "

0
source

Assuming you are referring to HEAD with your "last commit" 5 , you can also use this strategy for your task. I found that it is most effective so far:

  • Apply your changes from the old commit to your staging area without commit ( -n = --no-commit ):

     git cherry-pick -n <commit> 
  • Make all the changes you just made:

     git reset 
  • Compare what these changes are regarding your last commit:

     git diff HEAD 
  • Choose the necessary changes:

     git add -p 
  • And finally make a new commit:

     git commit -m "commit message" 

Source: adapted from this answer .

0
source

All Articles