How to Check What Commits are in One Git Branch But Not in the Other
Usually when comparing two branches we will use Git Diff Command that will show what lines of code are different between them.
Now, how do we do when we only want to see what commits we have in one branch that are not present in other branch?
So to get for
branch1 a list of commits that are missing from
branch2 we will use the Git Log Command.
git log can do that?
Yes it can… I was so surprised as you when I discovered this Git Tip in man pages, don’t believe me… just type in your terminal
man gitworkflows and look for example 5.
Comparing 2 Branches with Git Log
To get a list of commits from
branch2 that are not present in
branch1, just type in your terminal:
git log branch1..branch2
branch2 are not the same you will get a nice list, like this one:
Try Yourself this Git Tip
To reproduce exactly what I have done, just type the follwing commands:
mkdir -p Acme/Git/git-log cd Acme/Git/git-log ls -al git init echo "commit 1 on master." > readme.md && git add . && git commit -m 'Big Bang :)' git checkout -b branch1 echo "commit 2 on branch1." >> readme.md && git commit -am 'commit 2 on branch1' git checkout -b branch2 echo "commit 3 on branch2." >> readme.md && git commit -am 'commit 3 on branch2' echo "commit 4 on branch2." >> readme.md && git commit -am 'commit 4 on branch2' git checkout - echo "commit 5 on branch1." >> readme.md && git commit -am 'commit 5 on branch1' echo "commit 6 on branch1." >> readme.md && git commit -am 'commit 6 on branch1' echo "commit 7 on branch1." >> readme.md && git commit -am 'commit 7 on branch1' echo "commit 8 on branch1." >> readme.md && git commit -am 'commit 8 on branch1' git log branch1..branch2 git log branch2..branch1
Your terminal window should look like this now:
Output to Compare Branch 1 with Branch 2
Output to Compare Branch 2 with Branch 1
Did you Spotted?
In the list of commands I have executed their is one more good Git Tip… Are you able to spot which one I am talking about?
If not head over here to checkout this awesome Git Tip for git command…
Disclaimer: What I expressed here is only in my behalf and doesn't represent the company I work for, or any previous one, neither my family, friends, colleagues or anyone else unless I explicitly say so.