2

I am looking to list all the commits that were merged to the main branch from feature branches:-

    |
    v
    main branch
    |
    a  
    | \ 
    b  y
    |  |
    c  x
    | / -> feature branch
    d
    |

Is it possible to list the commit x and y, given the feature branch name and master main branch name?

LeGEC
  • 46,477
  • 5
  • 57
  • 104
rahul Kushwaha
  • 2,395
  • 7
  • 32
  • 64

1 Answers1

2

One way to do that is :

  1. spot commit d
  2. get the list of commits d..y

To spot commit d when branches are already merged, you can use the following script :

#!/bin/bash

target=$1
if [ -z "$target" ]; then
    target=HEAD
fi

# get the 'first-parent' list of commits from main
# (the left most path in your graph)
git rev-list --first-parent main > /tmp/tmp-rev-list

# get the 'first-parent' list of commits from $target,
# and use grep to match them with the first-parent history of 'main'
base=$(git rev-list --first-parent "$target" | grep -F -f /tmp/tmp-rev-list | head -1)

rm /tmp/tmp-rev-list

echo $base

or the one liner :

base=$(git rev-list --first-parent [y] |\
    grep -Ff <(git rev-list --first-parent main) |\
    head -1)

You can then run :

git log --oneline --graph $base..[y]

some explanations on why you need that:

Before branches are merged, you can look for the so called merge base of two branches : git merge-base main feature or git merge-base main y. But after feature has been merged, this stops working -- since feature is now in the history of main, this command will return the hash of y.

One way to go around this is the one described above: when inspecting the history of main, only follow the leftmost path. This is as close as you will get, with git, for "the list of commits that were created directly on main".

LeGEC
  • 46,477
  • 5
  • 57
  • 104
  • pointing to an answer I wrote to another question: [How do I get a list of all git commits to a merged branch?](https://stackoverflow.com/q/61617625/86072) – LeGEC Jan 19 '23 at 07:53