日本語

How to Find the Fork Point of Two Branches with git merge-base

​​Git​command line

This article was translated from the Japanese original with AI. 日本語の原文を読む

The commit where two branches split apart can be found with git merge-base main feature/search. It prints the hash of the closest common ancestor, the one nearest to both branch tips when you follow parents back from each. This is useful before reviewing a pull request or rebasing, when you want to confirm where the branch split off and what it added after that point. I ran the git commands in a test environment and include the output from before and after the check exactly as it appeared.

Note

The test environment was Ubuntu 24.04 with Git 2.43.0. The author name and email were example values (Neko <neko@example.com>), and commit dates were set with environment variables. I used a configuration that does not use a pager. I did not test interaction with remote repositories (updating origin/main), and I did not test on Windows or macOS.

History before the check: main and feature/search have diverged

I made two commits on main and then created the feature/search branch. After that, I added two commits to feature/search, "Add search screen" and "Add narrowing to search", and two commits to main, "Add usage heading to README" and "Add password field to login screen". Viewed with git log --graph, it looks like this:

console
$ git log --oneline --graph --all
* 3692910 ログイン画面にパスワード欄を追加
* c0131cd READMEに使い方の見出しを追加
| * 96a7a06 検索に絞り込みを追加
| * 483bc38 検索画面を追加
|/  
* f20c99c ログイン画面を追加
* e563966 最初のコミット

The f20c99c just below the |/ is where the two branches split. For a history this small, the graph makes it clear, but the graph becomes hard to read as the number of commits grows.

git merge-base prints the fork point

When I passed the two branch names, it printed the fork-point hash as one full 40-character line. It is the same commit as f20c99c in the graph. Swapping the two names gave the same result.

console
$ git merge-base main feature/search
f20c99c500d92a8fdef7b1a30fa0c4e99efbea13

$ git merge-base feature/search main
f20c99c500d92a8fdef7b1a30fa0c4e99efbea13

The official git merge-base reference calls this commit the "merge base." Among the common ancestors reached by following parents from both commits, it is the one with no other common ancestor on its descendant side (newer than it), so it is the closest common ancestor to the tips. In a three-way merge, it is also the commit used as the base for comparison.

A hash alone is hard to recognize, so passing it to another command with $(...) helps. Passing it to git log -1 shows its date and subject.

console
$ git log -1 --format="%h %ad %s" --date=short $(git merge-base main feature/search)
f20c99c 2026-11-02 ログイン画面を追加

Show only the commits and diffs after the fork point

Once you know the fork point, you can extract only what the branch added. Passing a range of the form "merge base..branch" to git log listed the two commits added on feature/search. In this history, main..feature/search gives the same two commits.

console
$ git log --oneline $(git merge-base main feature/search)..feature/search
96a7a06 検索に絞り込みを追加
483bc38 検索画面を追加

With git diff, the base you compare against changes the result a lot. Comparing the two branch tips directly with git diff main feature/search mixed in the README and login screen changes made on main, shown as removals in the opposite direction.

console
$ git diff --stat main feature/search
 README.md      | 2 --
 src/login.txt  | 1 -
 src/search.txt | 1 +
 3 files changed, 1 insertion(+), 3 deletions(-)

When the base is the merge base, only src/search.txt, the file changed on feature/search, remained. The same result came from main...feature/search with three dots and from --merge-base with the two branch names. To see only the changes the branch made, use any one of these three.

console
$ git diff --stat $(git merge-base main feature/search) feature/search
 src/search.txt | 1 +
 1 file changed, 1 insertion(+)

$ git diff --stat main...feature/search
 src/search.txt | 1 +
 1 file changed, 1 insertion(+)

$ git diff --stat --merge-base main feature/search
 src/search.txt | 1 +
 1 file changed, 1 insertion(+)

The official git diff reference also says that git diff A...B and git diff --merge-base A B are both the same as git diff $(git merge-base A B) B. The difference from git diff main..feature/search, which uses two dots, is explained with diagrams in another article, "How to compare working tree, staged, and committed differences with git diff."

–is-ancestor checks whether one commit is an ancestor of another

With --is-ancestor, nothing is printed, and the answer is given by the exit code. If the first commit is an ancestor of the second, the exit code is 0; otherwise it is 1. While the branches are still diverged, the tip of main is not an ancestor of feature/search, so the result was 1. The merge base is an ancestor of main, so the result was 0.

console
$ git merge-base --is-ancestor main feature/search; echo "終了コード: $?"
終了コード: 1

$ git merge-base --is-ancestor $(git merge-base main feature/search) main; echo "終了コード: $?"
終了コード: 0

Passing a branch name that does not exist printed an error and ended with exit code 128. The official reference says that on error the exit code is a nonzero value other than 1. If a shell script treats only 1 as "not an ancestor," it can tell a mistyped name apart from that case.

console
$ git merge-base --is-ancestor nosuchbranch main; echo "終了コード: $?"
fatal: Not a valid object name nosuchbranch
終了コード: 128

The official reference also suggests using --is-ancestor in place of the older approach in scripts that checks whether "the merge base equals A, so A is an ancestor of B."

After merging main, the tip of main becomes the merge base

I merged main into feature/search and then checked again. The graph now looks like this:

console
$ git log --oneline --graph --all
*   3e5b19d Merge branch 'main' into feature/search
|\  
| * 3692910 ログイン画面にパスワード欄を追加
| * c0131cd READMEに使い方の見出しを追加
* | 96a7a06 検索に絞り込みを追加
* | 483bc38 検索画面を追加
|/  
* f20c99c ログイン画面を追加
* e563966 最初のコミット

In the graph, f20c99c below |/ still looks like the place where the branches split. However, the result of git merge-base changed to the tip of main (3692910). This is because the tip of main can now be reached from feature/search as a parent of the merge commit.

console
$ git merge-base main feature/search
36929103846ba456cbc4976bd57c98b5952b4cac

$ git rev-parse main
36929103846ba456cbc4976bd57c98b5952b4cac

$ git merge-base --is-ancestor main feature/search; echo "終了コード: $?"
終了コード: 0

git merge-base does not print the commit where the branches first split. It prints the closest common ancestor at the current point in history. If you want to know the original fork point after merging, check it before the merge, or confirm it from the history graph.

After recreating main, use –fork-point to find the real fork point

Suppose you commit to main, undo it with git reset --hard, and then create a different commit. The fork point of a branch created on top of the undone commit cannot be found with plain git merge-base. In my test, I committed "Add settings screen (tentative)" to main, created feature/theme on top of it, and committed "Add theme switching". Then I moved main back one commit and created "Add settings screen" again.

console
$ git log --oneline --graph main feature/theme
* 88918d5 設定画面を追加
| * 646d2e6 テーマの切り替えを追加
| * 8501ffc 設定画面を追加(仮)
|/  
* 3692910 ログイン画面にパスワード欄を追加
* c0131cd READMEに使い方の見出しを追加
* f20c99c ログイン画面を追加
* e563966 最初のコミット

git merge-base looked only at the shape of the history and printed 3692910. If you take the range from this result, the undone "Add settings screen (tentative)" is included in the commits that feature/theme added.

console
$ git merge-base main feature/theme
36929103846ba456cbc4976bd57c98b5952b4cac

$ git log --oneline $(git merge-base main feature/theme)..feature/theme
646d2e6 テーマの切り替えを追加
8501ffc 設定画面を追加(仮)

With --fork-point, git also looks at the reflog of main (the record of which commits a branch tip pointed to over time). It then printed 8501ffc, the tip of main when feature/theme was created. The reflog keeps the record of when main pointed to 8501ffc.

console
$ git reflog main
88918d5 main@{0}: commit: 設定画面を追加
3692910 main@{1}: reset: moving to HEAD~1
8501ffc main@{2}: commit: 設定画面を追加(仮)
3692910 main@{3}: commit: ログイン画面にパスワード欄を追加
c0131cd main@{4}: commit: READMEに使い方の見出しを追加
f20c99c main@{5}: commit: ログイン画面を追加
e563966 main@{6}: commit (initial): 最初のコミット

$ git merge-base --fork-point main feature/theme
8501ffcaa4681ff3712633fcc04b1cfad889b46d

$ git log --oneline $(git merge-base --fork-point main feature/theme)..feature/theme
646d2e6 テーマの切り替えを追加

When I passed this result to git rebase --onto, only the one commit that feature/theme added moved onto the recreated main. The undone "Add settings screen (tentative)" did not remain on feature/theme.

console
$ git rebase -q --onto main $(git merge-base --fork-point main feature/theme) feature/theme

$ git log --oneline --graph main feature/theme
* b7d1d9f テーマの切り替えを追加
* 88918d5 設定画面を追加
* 3692910 ログイン画面にパスワード欄を追加
* c0131cd READMEに使い方の見出しを追加
* f20c99c ログイン画面を追加
* e563966 最初のコミット

The official reference explains this usage with an example where the tip of a remote-tracking branch (origin/master) moves to a different commit and the previous tip is discarded. In my test, I created the same kind of history with a local main.

Warning

According to the official reference, --fork-point depends on the reflog. If old reflog entries have been removed by git gc, it fails to find the fork point. It also cannot find it when the branch was created from a commit earlier than the tip of main.

In a history where branches merged each other, use –all to show every base

In a history where two branches have merged each other (a criss-cross merge), there can be two or more merge bases. In a separate test repository, I committed once on main and once on topic. Then I merged topic's first commit into main, and main's first commit into topic. I tagged each first commit (m1 and t1) before merging, so the tag names appear in the merge commit subjects. Listing the parent hashes with %p shows that both merge commits have the same two commits as parents.

console
$ git log --format="%h 親: %p  %s" --all
e529bfd 親: 70c4794 0a84819  Merge tag 'm1' into topic
888bb84 親: 0a84819 70c4794  Merge tag 't1'
0a84819 親: 1e67e2e  mainで1件目
70c4794 親: 1e67e2e  topicで1件目
1e67e2e 親:   最初のコミット

Run without options, it printed one base. With --all, it printed two.

console
$ git merge-base main topic
0a84819b4f270a0d70e01309ccd510016c48a3e6

$ git merge-base --all main topic
0a84819b4f270a0d70e01309ccd510016c48a3e6
70c47940e5a38a21f89e4363d80cb853754bbd1c

According to the official reference, when there are two or more merge bases, which one is printed without --all is not defined.

​Posted by 86