日本語

How to Show Only Merge Commits with git log –merges

​​Git​command line

Adding git log --merges shows only the merge commits in the history. It is useful when you want to look back at when and which branches were merged into main, or when you want to check the list of branches pulled in before a release. I also confirmed with real output that adding --first-parent limits the list to merges into main, and that branches merged by fast-forward do not appear.

Note

The test environment was Ubuntu 24.04 with Git 2.43.0. I set the author name and email to example values (Neko <neko@example.com>), and fixed the commit dates with environment variables so the output stays the same every time. I did not test merges made on GitHub or other hosting services, and I did not test on Windows or macOS.

The test repository

I prepared a repository in which three branches were merged into main in different ways. fix/typo was merged by fast-forward, and feature/header and feature/search were merged with --no-ff, which creates a merge commit. In feature/search, I pulled main in once during the work.

Plain git log --oneline shows regular commits and merge commits mixed together.

console
$ git log --oneline
58c04db 写真のページを作る
4827be8 Merge branch 'feature/search'
2a49783 検索ボタンを付ける
2e1c378 Merge branch 'main' into feature/search
0a2773b フッターを作る
e4abb2c 検索欄を作る
ccc125b Merge branch 'feature/header'
19516f9 ヘッダーにメニューを入れる
d87c660 ヘッダーを作る
9d6aabb 見出しの誤字を直す
e68972d トップページを作る

The shape of the branches becomes clear with --graph.

console
$ git log --oneline --graph
* 58c04db 写真のページを作る
*   4827be8 Merge branch 'feature/search'
|\  
| * 2a49783 検索ボタンを付ける
| *   2e1c378 Merge branch 'main' into feature/search
| |\  
| |/  
|/|   
* | 0a2773b フッターを作る
| * e4abb2c 検索欄を作る
|/  
*   ccc125b Merge branch 'feature/header'
|\  
| * 19516f9 ヘッダーにメニューを入れる
| * d87c660 ヘッダーを作る
|/  
* 9d6aabb 見出しの誤字を直す
* e68972d トップページを作る

Show only merge commits with –merges

On the same repository, --merges shows only the 3 merge commits out of the 11 commits.

console
$ git log --merges --oneline
4827be8 Merge branch 'feature/search'
2e1c378 Merge branch 'main' into feature/search
ccc125b Merge branch 'feature/header'

Without --oneline, the output has the usual git log format. A merge commit has a Merge: line that lists its two parent commits.

console
$ git log --merges
commit 4827be853934a9cce44930544f696c319c93128a
Merge: 0a2773b 2a49783
Author: Neko <neko@example.com>
Date:   Thu Nov 12 10:11:00 2026 +0900

    Merge branch 'feature/search'

commit 2e1c378c3dd6b3db3998b1506ec63a037e5a6757
Merge: e4abb2c 0a2773b
Author: Neko <neko@example.com>
Date:   Thu Nov 12 10:09:00 2026 +0900

    Merge branch 'main' into feature/search

commit ccc125b9db504e6b155c9500703386fb42aff03a
Merge: 9d6aabb 19516f9
Author: Neko <neko@example.com>
Date:   Thu Nov 12 10:06:00 2026 +0900

    Merge branch 'feature/header'

The official git log reference says that --merges shows only merge commits and is exactly the same as --min-parents=2 (show only commits with two or more parents). I ran --min-parents=2 and got the same 3 commits.

console
$ git log --min-parents=2 --oneline
4827be8 Merge branch 'feature/search'
2e1c378 Merge branch 'main' into feature/search
ccc125b Merge branch 'feature/header'

Branches merged by fast-forward do not appear

I also merged fix/typo into main, but it is not in the --merges list. When I merged it, the output showed Fast-forward.

console
$ git switch -q main && git merge fix/typo
Updating e68972d..9d6aabb
Fast-forward
 index.html | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

According to the official git merge reference, a fast-forward merge only moves the branch pointer forward and does not create a merge commit. What entered main was 9d6aabb itself, which was created on fix/typo, and it has only one parent. (%p is the format that prints the short hashes of the parent commits.)

console
$ git log -1 --format="%h %p %s" 9d6aabb
9d6aabb e68972d 見出しの誤字を直す

If you want every merge to be visible with --merges, merge with git merge --no-ff, as I did for feature/header and feature/search. The official reference says that --no-ff creates a merge commit even when a fast-forward would be possible.

Limit the list to merges into main

The --merges list also included 2e1c378 Merge branch 'main' into feature/search. This is a merge that brought main into feature/search during its work. It did not merge anything into main.

Adding --first-parent removed that one entry, leaving only the merges into main.

console
$ git log --merges --first-parent --oneline
4827be8 Merge branch 'feature/search'
ccc125b Merge branch 'feature/header'

--first-parent tells Git to follow only the first parent when it reaches a merge commit (official git log reference). When you merge while main is checked out, the first parent is the tip of main from before the merge. Printing the parent hashes shows that the first parent of 4827be8 is 0a2773b on the main side, and the second parent is 2a49783, the tip of feature/search.

console
$ git log --merges --first-parent --format="%h %p %s"
4827be8 0a2773b 2a49783 Merge branch 'feature/search'
ccc125b 9d6aabb 19516f9 Merge branch 'feature/header'

If you follow only the first parent from main, 2e1c378 is not on that path, so it does not appear. The history looks like this:

A diagram of the commit history arranged vertically, with merge commits colored
The 3 merge commits shown by –merges. The pink one does not appear when –first-parent is used

See the commits brought in by one merge

Once you know a merge commit, you can also see the commits that merge brought into main. 4827be8^- is the notation from the official revision specification. It is a shorthand for 4827be8^1..4827be8, meaning the commits reachable from 4827be8 but not from its first parent.

console
$ git log --oneline 4827be8^-
4827be8 Merge branch 'feature/search'
2a49783 検索ボタンを付ける
2e1c378 Merge branch 'main' into feature/search
e4abb2c 検索欄を作る

The output includes the merge commit itself and the commits made on feature/search. The merge that brought main into the branch is also included, so if you want only the commits made on the branch, add --no-merges.

console
$ git log --oneline --no-merges 4827be8^-
2a49783 検索ボタンを付ける
e4abb2c 検索欄を作る

--no-merges is the opposite of --merges. It leaves out merge commits. Applied to the whole history, the 3 merge commits disappear and 8 commits remain.

console
$ git log --no-merges --oneline
58c04db 写真のページを作る
2a49783 検索ボタンを付ける
0a2773b フッターを作る
e4abb2c 検索欄を作る
19516f9 ヘッダーにメニューを入れる
d87c660 ヘッダーを作る
9d6aabb 見出しの誤字を直す
e68972d トップページを作る

List the files changed by a merge

--stat lists the files changed by each commit. However, --merges with --stat alone did not show the list of files for a merge commit.

console
$ git log --merges --stat --format="%h %s" -1
4827be8 Merge branch 'feature/search'

According to the official git log reference, a merge commit's diff is not shown unless you specify something like --diff-merges. However, when --first-parent is given, the default is to show the diff against the first parent. With --first-parent added, the output showed which files in main changed in each merge.

console
$ git log --merges --first-parent --stat --format="%h %s"
4827be8 Merge branch 'feature/search'

 search.html | 2 ++
 1 file changed, 2 insertions(+)
ccc125b Merge branch 'feature/header'

 header.html | 2 ++
 1 file changed, 2 insertions(+)

Find merges by branch name

The merge commits that git merge creates include the branch name in the message, such as Merge branch 'feature/header'. --grep shows only the commits whose message matches a pattern. Combined with --merges, it finds the merge commits for a specific branch.

console
$ git log --merges --oneline --grep="feature/header"
ccc125b Merge branch 'feature/header'

You can use the hash you find directly in a revision such as 4827be8^-, shown in the earlier section.

​Posted by 86