Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git diff but only for changes on another branch

Tags:

git

Suppose I have a branch0 in git.

From this branch0 I create a branch1 and start working on it. After a while I committed changes to this branch1. Meanwhile branch0 is being changed.

If I compare the latest version of branch1 against branch0 I will see all the changes between the branches, not only the changes that correlate to the changes in branch1.

For example, if a file was modified in the branch0, but the file was not modified on branch1, the git diff will show me the changes to the file. However the file was not modified on branch1. So I would like it to be ignored in the diff.

In a sense, I'd like to do a git diff between branch1 and branch0, but only for the files that were modified in branch1.

Is it possible in git? I can imagine doing it in bash, listing all the files and doing a diff on a one-by-one. But wanted to know if there's an easier way in git.

like image 441
Alexandre Santos Avatar asked Jul 20 '26 12:07

Alexandre Santos


2 Answers

git diff between branch1 and branch0, but only for the files that were modified in branch1.

That's

git diff branch0 branch1 -- $(git diff --name-only branch0...branch1)

The three-dots form used above to generate the filenames is

git diff [--options] <commit>...<commit> [--] [<path>…]

changes on the branch containing and up to the second , starting at a common ancestor of both . "git diff A...B" is equivalent to "git diff $(git-merge-base A B) B". You can omit any one of , which has the same effect as using HEAD instead.

like image 117
jthill Avatar answered Jul 22 '26 00:07

jthill


git diff branch0...branch1

will do what you want (note the 3 dots)

That is, it will show you only the changes made on branch1, since the common ancestor commit. The common ancestor commit, is the most recent commit made on both branches.

In the tree below, git diff branch0...branch1 will show you changes due to commits A, B, C. The common ancestor is E.

     A---B---C branch1
     /
D---E---F---G branch0
like image 40
Jason S Avatar answered Jul 22 '26 00:07

Jason S



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!