diff branch changes in git relative to common ancestor
If you are going to check the current branch against where you branch off, just add ...
. So the command would look like
git diff master...
It's the same as git diff master...HEAD
. I am running git version 2.17.1. This might behave differently in an older version.
After extensively looking at git help rev-parse
and experimenting around, I found this piece of information:
<rev1>..<rev2>
Include commits that are reachable from <rev2> but exclude those
that are reachable from <rev1>. When either <rev1> or <rev2> is
omitted, it defaults to HEAD.
<rev1>...<rev2>
Include commits that are reachable from either <rev1> or <rev2>
but exclude those that are reachable from both. When either <rev1>
or <rev2> is omitted, it defaults to HEAD.
Somehow I was always under the impression, that master..branch
is what I need and forgot about master...branch
(with three dots).
But experimenting with it showed, that the three-dot notation is exactly what I'm looking for:
$ git diff master...branch
shows only the differences of branch
relative to where it took off of master
.
What I'm looking for is basically a nicer way to run
$ git diff $(git merge-base master branch)..branch
How about creating an alias?
git config alias.diffbr '!f() { git diff $(git merge-base master $1)..$1; }; f'
Then you can simply do git diffbr branch
.
If you want the alias to be available for all repositories on your machine add the --global
argument to git config
.
I know this is not the answer you're looking for here, but it might help someone else. Using a graphical tool like gitk, you can:
- Open gitk
- Click on H, right click and select "Mark this commit"
- Click on B, right click and select "Diff this -> marked commit"
Now you see all updated files to the right, and all the modified lines (per file) to the left.