Git Tip - How to See What Commits are Different Between Branches

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.

What 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

So if branch1 and branch2 are not the same you will get a nice list, like this one:

git log shows commits difference between branch 1 and branch 2

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:

All Commands Execute on the Console

Output to Compare Branch 1 with Branch 2

git log shows commits difference between branch 1 and branch 2

Output to Compare Branch 2 with Branch 1

git log shows commits difference between branch 2 and 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.

Keeping me Motivated

Was this post useful to you? Did you enjoyed reading it? Did you learned something new?

If so, in return I only ask you to show your appreciation by Sharing It:

Don't miss my next article. To stay on the loop, just Follow Me at:

If you are feelling really generous you can always offer me a coffee or a beer.

Your actions will keep me MOTIVATED to continue writing more Blog Posts 😃

Comments

Disqus is disabled on my Blog because I don't agree with their policies.