Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a list of different commits between two git branches?

I want to see a list of only non-common commits between two branches.

How can I get output like this?

Basically a git diff -y master new-feature summary between two branches:

              master                               new-feature
-------------------------------------|--------------------------------------
xxx - Jan 1st 2018 - initial commit  | xxx - Jan 1st 2018 - initial commit
xxx - Feb 1st 2018 - fix a bug       | xxx - Feb 1st 2018 - fix a bug
                                     > xxx - Mar 1st 2018 - WIP almost done
xxx - Apr 1st 2018 - fix another bug | xxx - Apr 1st 2018 - fix another bug
xxx - May 1st 2018 - fix more bugs   | xxx - May 1st 2018 - fix more bugs
                                     > xxx - Jun 1st 2018 - Ready to merge!
xxx - Jul 1st 2018 - latest patches  < 

Solution: I don't know how to make heads or tails of it, but I think git log --graph accomplishes this, just with a very confusing visual style:

git log --left-right --graph --cherry-pick --oneline master





Or this?

Better yet, git diff -y --suppress-common-lines master new-feature is what I really want:

              master                               new-feature
-------------------------------------|--------------------------------------
                                     > xxx - Mar 1st 2018 - WIP almost done
                                     > xxx - Jun 1st 2018 - Ready to merge!
xxx - Jul 1st 2018 - latest patches  <

Solution: You probably can't with git alone but, as per @torek's answer, you can get mostly there with git rev-list:

git rev-list --left-right master...new-feature

>eb57618eed654685a7999da847628dc5eb27313f
>0da0196ab24542a1a1697b1c0c5efeef6d09e027
>9452f57d97e5fc86e773e7a9fca488b7a0d44a0c
<4fc12fe25a127f8f0dfddf7625c22656c7b2c7c1
<9c0058dcabacfc6560f4fbaf73ea55dd70d27036
>5117fcd041793898683f9469aac00337e9fadd8b





Or even just this?

And more critical than that, a git diff --right-only master new-feature style view would get the job done:

     commits only in new-feature
-------------------------------------
xxx - Mar 1st 2018 - WIP almost done
xxx - Jun 1st 2018 - Ready to merge!

Which, of course, could be reversed to get the opposite view git diff --right-only new-feature master

       commits only in master               
-------------------------------------
xxx - Jul 1st 2018 - latest patches 

Solution: As @brentjanderson points out, git log main..new-feature and git cherry -v main both do this:

git cherry -v main

+ c00335cd93898683f9469aafad8a8476227b117f WIP do things
+ f5c86e777d97e5f3e7a9fca488b79a0d44ac9452 WIP do more
+ 0ef6ddda576180196ab7b1c0c57eefe009e027dc Finished!

as well as

git log master..new-feature --format="%h - %ad - %s" --date=format:'%b %d %Y'

c00335cd - Jan 01 2018 - WIP do things
f5c86e77 - Feb 01 2018 - WIP do more
0ef6ddda - Mar 01 2018 - Finished!


P.S. I have tried git checkout new-feature; git log --left-right --graph --cherry-pick --oneline master before, but I can't make heads or tales (or tails) of the output. Actually, on second thought, I bet taking the output, turning it sideways, and playing it as excitebike map would make a good tale of it. Hmm...

like image 833
coolaj86 Avatar asked Nov 01 '18 18:11

coolaj86


1 Answers

Here is a basic variant without fancy formatting:

git log master..new-feature

You can get "left side" or "right side" data by swapping the order of the branches:

git log left-side-branch..right-side-branch

This works with other refs (commit hashes, remote branches, tags) too.

To get the full date formatting you want:

git log master..new-feature --format="%h - %ad - %s" --date=format:'%b %d %Y'

This doesn't do ordinal dates (Mar 1st 2018 is Mar 01 2018) because strftime does not support that. If that's a hard requirement, I suggest writing a batch script.

like image 157
brentjanderson Avatar answered Nov 06 '22 21:11

brentjanderson