1

In git, one can use the following command to get a list of branch commits for some branch, xxxx, that have not been merged into master:

git rev-list xxxx --not master

For example, if there are 3 commits in branch xxxx, you will get a list of 3 hashes on 3 lines:

$ git rev-list xxxx --not master
<hash1>
<hash2>
<hash3>

If I merge this branch into master, however, I can no longer retrieve this list of hashes with the same command; I get an empty list, because the commits are now in master.

I was wondering if anyone out there has a command that can generate this list post-merge. Preferably, the command would work in the same manner whether or not the commits had been merged to master.

The following essentially does the trick:

git rev-list xxxx --not <hash3>^

However, I want to use this in a script, and I don't want to have to provide <hash3> as an argument - I need to find it programmatically.

wickstopher
  • 981
  • 6
  • 18
  • 1
    What do you want to achieve with the script? There might be an easier way. – Sven Marnach Sep 24 '15 at 13:10
  • I'm open to that possibility. The purpose of the script is to generate a unified diff view link for Trac (http://trac.edgewall.org/). The script needs to produce the same output link given a particular remote branch regardless of whether or not that branch has been merged. – wickstopher Sep 24 '15 at 13:12
  • Directly after the merge `git rev-list master^..xxxx` should be what you are looking for. If `master` isn't pointing directly to the merge commit anymore, it becomes more difficult. – Sven Marnach Sep 24 '15 at 13:33
  • Yep. Said difficulty is what I'm trying to overcome here. I would like the same list of hashes regardless of where master is at the time the script is called. – wickstopher Sep 24 '15 at 13:36

1 Answers1

0

Found the solution for the "oldest ancestor" problem in https://stackoverflow.com/a/4991675/515973 and https://stackoverflow.com/a/30563070/515973.

git rev-list $(diff -u1 <(git rev-list --first-parent xxxx) <(git rev-list --first-parent master)|sed -ne 's/^ //p')..xxxx

Or if you create the useful "oldest-ancestor" alias,

oldest-ancestor = !bash -c 'diff -u1 <(git rev-list --first-parent "${1:-master}") <(git rev-list --first-parent "${2:-HEAD}") | sed -ne \"s/^ //p\"' -

then the solution is:

git rev-list $(git oldest-ancestor master xxxx)..xxxx

I tested and it works fine with merged and unmerged branches, but not with a fast-forward merge.

Community
  • 1
  • 1
Julien Carsique
  • 3,915
  • 3
  • 22
  • 28