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 merge-base finds the commit where two branches split

Git
~/tips/tip-git-merge-base-fork-point

git merge-base main feature

Prints the common ancestor of two branches, handy for diffs, rebases and scripts.

Every time a tool says “compared with where you branched off”, it means the merge base. You can ask git for that commit directly.

git merge-base main feature

The output is one full commit hash: the best common ancestor of the two branches. I find it most useful inside other commands. For example, to see all changes on your branch as one diff, including uncommitted work:

git diff $(git merge-base main HEAD)

That’s different from git diff main...HEAD, which only compares commits and ignores what you haven’t committed yet.

It also helps when you want to squash a whole branch by hand. git reset --soft $(git merge-base main HEAD) moves the branch back to the fork point and keeps all changes staged, ready for one new commit.

There’s a yes or no form as well: git merge-base --is-ancestor A B exits with 0 when A is an ancestor of B. That’s handy in a script that should only continue when a fix is already in a branch.

After you’ve merged main into your branch a few times, the merge base moves up to the last merged main commit, not the original branch point. That’s usually what you want, but it can be a surprise the first time. In rare criss-cross histories there can be more than one best ancestor; --all prints them all.

Further reading: git-merge-base 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