27

I've two branches, master and live. Master is the development branch and contains commits that aren't ready to go into live.

  • If a change is made in the master branch that needs to go into the live branch then they're cherry picked from master to live.
  • Sometimes commits are made to the live branch that are then either cherry picked into master or merged into master.

What I want to be able to do is view all the commits that are in master than aren't in live, I've had a good search on here and Google and I was using this:

git log --cherry-pick --no-merges --oneline master...live

This worked until I merged live into master. But now it lists several commits that are in both.

So what is the correct command to view all commits that are in the master branch and not in the live branch?

JJD
  • 50,076
  • 60
  • 203
  • 339
Rwky
  • 2,116
  • 1
  • 18
  • 22
  • Have you looked at `git cherry` (which is completely different from `git cherry-pick`)? I haven't played with it a lot, so I don't know how it works post-merge, but it is supposed to "find commits not merged upstream". I think in your case it would be `git cherry live master`. – twalberg Dec 18 '12 at 20:21
  • I've tried that too and that sadly doesn't work either. It prints a similar list to the original command in the question. – Rwky Dec 18 '12 at 21:39
  • 1
    The *correct* solution here would be to use branches rather than cherry-picks, IMO. But the `--cherry-pick` argument should do what you want it to, and certainly seems to in the quick test I just did. Can you reproduce this in a test repository so we can see what's different between my test and your problem? – me_and Dec 19 '12 at 12:32
  • how about `git log master..live` (note the two dots instead of three), which will list all commits between where master points and where live points. – Nevik Rehnel Dec 19 '12 at 15:07
  • Did you maybe do a strange merge? Did you maybe cherry pick a commit from live into master that was already merged there? – Chronial Dec 20 '12 at 22:38
  • I do not think there is a magical git command that is able to do this as parents in both branches might be different (and thus SHAs too). Because I work with gerrit code review I wrote a script that searches for commit-ids generated by gerrit (part of the commit msg) and try to look them up in the other branch. – gvd Dec 21 '12 at 00:11
  • me_and I need to use cherry picks to merge bug fixes from one branch into the other. Nevik the two dots generate the same output as git cherry (see comment in answer below). Chronial, I didn't cherry pick an already merged commit, however I will have merged a branch a few days after cherry picking a commit from it. gvd can you share the script? – Rwky Dec 21 '12 at 17:17
  • @gvd – that’s not true. `git log --cherry-pick` is meant for exactly that: it does not compare commit SHAs, but diffs and thus identifies commits that introduced exactly the same change. Independent of parent commit, dates and commit message. – Chronial Dec 21 '12 at 23:57
  • @Chronial you are right, I was unaware of this switch. Thanks. – gvd Dec 23 '12 at 13:21
  • Can you set up a small example repository demonstrating your case? – michas Dec 24 '12 at 01:10
  • 1
    (You really should think about merges (from live to master) instead of cherry-picks.) – michas Dec 24 '12 at 01:12
  • Possible duplicate: http://stackoverflow.com/questions/7566416/different-commits-between-two-branches – angularsen Jun 23 '14 at 11:24

3 Answers3

27

You are very close. The correct command is:

git log --cherry-pick --oneline --no-merges --left-only master...live

from the log manpage:

--left-only, --right-only

List only commits on the respective side of a symmetric range, i.e. only those which would be marked < resp. > by --left-right.

For example, --cherry-pick --right-only A...B omits those commits from B which are in A or are patch-equivalent to a commit in A. In other words, this lists the + commits from git cherry A B. More precisely, --cherry-pick --right-only --no-merges gives the exact list.

Community
  • 1
  • 1
Chronial
  • 66,706
  • 14
  • 93
  • 99
  • Sadly that doesn't work either, that lists even more commits that I know are in both branches. – Rwky Dec 20 '12 at 19:13
  • Can you show us the output of `git show **commit-id** | git patch-id` of two of these commits that are the same? – Chronial Dec 20 '12 at 19:25
  • These are two commits which are identical (one is a cherry pick) git show a0d2346 | git patch-id 71c61dadd2e31e3b17606c28e937668c9fb2b313 a0d2346cea23d823a63218d85dfb8ff567b3c678 git show c0a0579 | git patch-id 71c61dadd2e31e3b17606c28e937668c9fb2b313 c0a0579263f221e2113436ff844ac862df77b82e a0d2346 appears in the output of the command in the above answer – Rwky Dec 20 '12 at 21:56
  • mhh – what the output of `git cherry` suggest? Does git realize that the commits are duplicates there? – Chronial Dec 20 '12 at 22:35
  • git cherry with no arguments outputs nothing, git cherry live master throws out 34 commits most of which I know are in both branches, git cherry master live throws out one commit which I know is in both branches. – Rwky Dec 21 '12 at 17:13
  • At least the manpage Chronial posted documents that **patch-equivalent** commits are recognized. This should include cherry-picked commits (with different SHA) as I understand it. – JJD Dec 21 '12 at 23:24
  • The `git cherry` command prefixes every commit with a symbol – `+` or `-`, that shows if it’s an original commit or if it’s cherry picked from the other branch – are these symbols correct? – Chronial Dec 21 '12 at 23:55
4

git log master ^live --no-merges

Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
  • That produces the same output as the above command (assuming I add --oneline to your command) – Rwky Dec 13 '12 at 11:42
2

I ended up solving my own problem using a short python script combining git patch-id and git log, the script can be found here https://gist.github.com/4565584

Rwky
  • 2,116
  • 1
  • 18
  • 22
  • How can I run this script? I got this error: `[User]$ python3 compare-git-branch.py -a main -b feature -t yesterday Traceback (most recent call last): File "compare-git-branch.py", line 33, in branch1_lines = branch1_output.split('\n') TypeError: a bytes-like object is required, not 'str'` – akshay_sushir Mar 09 '23 at 05:29