简体   繁体   中英

git log to graphically show all commits on 2 branches, but only show most recent shared commit

I have 2 branches in git which have diverged a little. I want to display the commits on both. with git log --oneline --graph --decorate branch1 branch2 I can see the 2 branches, their history (graphically), and where they diverge. But this display shows all commits.

How can I do the above command, but stop at the first commit these 2 branches have in common? ie I want the last line of the output to be the commit which they have in common, and then see (in the graphical ASCII art way that git log --graph shows) the branches diverging.

git version 2.17.1 on Ubuntu Linux 18.04

git log --oneline --graph --decorate branch1...branch2 --boundary

branch1...branch2 refer to the commits that come after the nearest common commit (excluding the common commit). --boundary prints the nearest common commit (the boundary commit) with o instead of * .

Or

git log --oneline --graph --decorate branch1 branch2 ^$(git merge-base branch1 branch2)^

$(git merge-base branch1 branch2) refers to the nearest common commit. C^ refers to the first parent of C . ^C means to exclude all the commits that come before C including C itself. ^C^ means to exclude all the commits that come before C^ including C^ itself.

The 1st command is suggested. When the common commit is a merge commit, we need to exclude its other parent commit(s), with ^$(git merge-base branch1 branch2)^2 , ^$(git merge-base branch1 branch2)^3 , and so on. Or with a much long command to exclude all the ancestors of the common commit.

git log --oneline --graph --decorate branch1 branch2 ^$(echo $(git log -1 --pretty=%P $(git merge-base branch1 branch2)) | sed -e 's/ / ^/g')

After searching the doc , the above command could be shortened to

git log --oneline --graph --decorate branch1 branch2 ^$(git merge-base branch1 branch2)^@

C^@ refers to all the parents of C.

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