简体   繁体   中英

How to list the commits of a branch which is now merged into main?

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?

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 ".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM