brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: git log with ancestry-path shows how a commit reached main

Git
~/tips/tip-git-log-ancestry-path-merged

git log --oneline --ancestry-path --merges abc1234..main

Finds the merge that brought a specific commit into a branch.

Knowing that a fix is on main is one thing. Finding the merge that brought it there, with the pull request number in its message, takes one more option.

git log --oneline --ancestry-path --merges abc1234..main

A normal abc1234..main range lists everything on main that isn’t in abc1234, including side branches that have nothing to do with it. --ancestry-path keeps only commits that are descendants of abc1234, the commits that sit on a path from it to main. --merges then leaves only the merge commits on that path.

The last line of the output is the oldest one, so that’s usually the merge that first pulled the commit in. To see only that one, I’d add --reverse and look at the first line:

git log --oneline --ancestry-path --merges --reverse abc1234..main | head -1

In a project that merges pull requests with merge commits, that line contains something like “Merge pull request #123”, and now you know where the discussion happened.

This needs actual merge commits. Projects that squash or rebase pull requests don’t have them, and the commit you’re looking for then doesn’t exist on main under the same hash at all. In that case search for the subject with git log --grep instead.

Further reading: git-log documentation

More Git tips git show-branch main feature git log --stat -5 git merge --no-ff feature -m "Merge feature" git merge --squash feature git merge --abort All Git tips (144) All topics

Link