日本語

How to View Per-Commit Changes with git log -p

​​Git​command line

Adding -p to git log prints the diff for each commit, with the changed lines, under the author, date, and message. You can read what changed in each commit, newest first, without running git show for each one. It is useful for reading recent changes before a code review, or for finding from the diffs when a bug was introduced.

Note

The test environment was Ubuntu 24.04 and 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. I captured the output with the pager turned off. I did not test on Windows or macOS, or how the output looks on GitHub or other web views.

The repository I tested

I made four commits on three files: README.md, menu.txt, and hours.txt. In the second commit I wrote the menu. In the third I changed the coffee price and added the opening-hours file. In the fourth I added the regular closing day.

Plain git log does not show diffs

Without any options, git log prints only each commit's hash, author, date, and message. It does not show which lines in which files changed.

console
$ git log
commit b73a8c156ad700111dd247324f0695846fa6cd7e
Author: Neko <neko@example.com>
Date:   Wed Nov 4 10:00:00 2026 +0900

    定休日を追加

commit 076169cd32a82d421ffd9142072203e3f4479ac8
Author: Neko <neko@example.com>
Date:   Tue Nov 3 10:00:00 2026 +0900

    値上げと営業時間

commit d6937c06152dd31d2eefc355fad4546f2803376d
Author: Neko <neko@example.com>
Date:   Mon Nov 2 10:00:00 2026 +0900

    メニューを追加

commit ac6f1a6d0b9faef84e00fe18769071801f22fc54
Author: Neko <neko@example.com>
Date:   Sun Nov 1 10:00:00 2026 +0900

    最初のコミット

The official git log reference also says that diffs are not shown by default.

Showing diffs in sequence with git log -p

With -p, each commit message is followed by a diff starting with diff --git. As without -p, the newest commit comes first.

console
$ git log -p
commit b73a8c156ad700111dd247324f0695846fa6cd7e
Author: Neko <neko@example.com>
Date:   Wed Nov 4 10:00:00 2026 +0900

    定休日を追加

diff --git a/hours.txt b/hours.txt
index e89129b..c1f5f87 100644
--- a/hours.txt
+++ b/hours.txt
@@ -1 +1,2 @@
 営業時間 11:00-18:00
+定休日 水曜日

commit 076169cd32a82d421ffd9142072203e3f4479ac8
Author: Neko <neko@example.com>
Date:   Tue Nov 3 10:00:00 2026 +0900

    値上げと営業時間

diff --git a/hours.txt b/hours.txt
new file mode 100644
index 0000000..e89129b
--- /dev/null
+++ b/hours.txt
@@ -0,0 +1 @@
+営業時間 11:00-18:00
diff --git a/menu.txt b/menu.txt
index c02f2d8..4aa77e8 100644
--- a/menu.txt
+++ b/menu.txt
@@ -1,2 +1,2 @@
-コーヒー 400円
+コーヒー 450円
 紅茶 400円

commit d6937c06152dd31d2eefc355fad4546f2803376d
Author: Neko <neko@example.com>
Date:   Mon Nov 2 10:00:00 2026 +0900

    メニューを追加

diff --git a/menu.txt b/menu.txt
new file mode 100644
index 0000000..c02f2d8
--- /dev/null
+++ b/menu.txt
@@ -0,0 +1,2 @@
+コーヒー 400円
+紅茶 400円

commit ac6f1a6d0b9faef84e00fe18769071801f22fc54
Author: Neko <neko@example.com>
Date:   Sun Nov 1 10:00:00 2026 +0900

    最初のコミット

diff --git a/README.md b/README.md
new file mode 100644
index 0000000..c28e4e4
--- /dev/null
+++ b/README.md
@@ -0,0 +1 @@
+# Cat cafe memo

Lines starting with + are added lines, and lines starting with - are removed lines. Lines starting with a space are unchanged context around the changes. In 076169c, which changed two files, the diffs for hours.txt and menu.txt appear one after the other. A newly created file is marked by the new file mode line and --- /dev/null.

The output opens in a pager such as less (core.pager documentation). If you want the output printed straight to the terminal without a pager, see my article on how to stop Git from opening output in a pager.

Looking at only the most recent few commits

With many commits, the git log -p output gets long quickly. Passing a number such as -2 limits the output to the newest two commits. -n 2 gave the same two commits.

console
$ git log -p -2
commit b73a8c156ad700111dd247324f0695846fa6cd7e
Author: Neko <neko@example.com>
Date:   Wed Nov 4 10:00:00 2026 +0900

    定休日を追加

diff --git a/hours.txt b/hours.txt
index e89129b..c1f5f87 100644
--- a/hours.txt
+++ b/hours.txt
@@ -1 +1,2 @@
 営業時間 11:00-18:00
+定休日 水曜日

commit 076169cd32a82d421ffd9142072203e3f4479ac8
Author: Neko <neko@example.com>
Date:   Tue Nov 3 10:00:00 2026 +0900

    値上げと営業時間

diff --git a/hours.txt b/hours.txt
new file mode 100644
index 0000000..e89129b
--- /dev/null
+++ b/hours.txt
@@ -0,0 +1 @@
+営業時間 11:00-18:00
diff --git a/menu.txt b/menu.txt
index c02f2d8..4aa77e8 100644
--- a/menu.txt
+++ b/menu.txt
@@ -1,2 +1,2 @@
-コーヒー 400円
+コーヒー 450円
 紅茶 400円

Viewing changes to one file only

If you put a file name after --, only the commits that changed that file are shown, and the diff covers only that file. 076169c also changed menu.txt, but only the hours.txt diff appeared.

console
$ git log -p -- hours.txt
commit b73a8c156ad700111dd247324f0695846fa6cd7e
Author: Neko <neko@example.com>
Date:   Wed Nov 4 10:00:00 2026 +0900

    定休日を追加

diff --git a/hours.txt b/hours.txt
index e89129b..c1f5f87 100644
--- a/hours.txt
+++ b/hours.txt
@@ -1 +1,2 @@
 営業時間 11:00-18:00
+定休日 水曜日

commit 076169cd32a82d421ffd9142072203e3f4479ac8
Author: Neko <neko@example.com>
Date:   Tue Nov 3 10:00:00 2026 +0900

    値上げと営業時間

diff --git a/hours.txt b/hours.txt
new file mode 100644
index 0000000..e89129b
--- /dev/null
+++ b/hours.txt
@@ -0,0 +1 @@
+営業時間 11:00-18:00

To follow a file back past a rename, use --follow, as described in my article on using git log --follow to trace a renamed file.

Making each commit header one line

Adding --oneline removes the author and date lines. Each commit then shows a single line of the short hash and message, followed by its diff.

console
$ git log -p --oneline
b73a8c1 定休日を追加
diff --git a/hours.txt b/hours.txt
index e89129b..c1f5f87 100644
--- a/hours.txt
+++ b/hours.txt
@@ -1 +1,2 @@
 営業時間 11:00-18:00
+定休日 水曜日
076169c 値上げと営業時間
diff --git a/hours.txt b/hours.txt
new file mode 100644
index 0000000..e89129b
--- /dev/null
+++ b/hours.txt
@@ -0,0 +1 @@
+営業時間 11:00-18:00
diff --git a/menu.txt b/menu.txt
index c02f2d8..4aa77e8 100644
--- a/menu.txt
+++ b/menu.txt
@@ -1,2 +1,2 @@
-コーヒー 400円
+コーヒー 450円
 紅茶 400円
d6937c0 メニューを追加
diff --git a/menu.txt b/menu.txt
new file mode 100644
index 0000000..c02f2d8
--- /dev/null
+++ b/menu.txt
@@ -0,0 +1,2 @@
+コーヒー 400円
+紅茶 400円
ac6f1a6 最初のコミット
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..c28e4e4
--- /dev/null
+++ b/README.md
@@ -0,0 +1 @@
+# Cat cafe memo

The output is shorter, but there is no blank line between commits. To find where one commit ends and the next begins, you have to look for the lines that start with a hash.

Listing changed files before the diff

--stat shows, for each commit, the changed files and the number of added and removed lines. It does not show the diff itself.

console
$ git log --stat --oneline -2
b73a8c1 定休日を追加
 hours.txt | 1 +
 1 file changed, 1 insertion(+)
076169c 値上げと営業時間
 hours.txt | 1 +
 menu.txt  | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)

When you combine -p and --stat, the diff follows right after the file list. You can see which files changed first, then read the contents. The official reference says that --patch-with-stat means the same as -p --stat.

console
$ git log -p --stat --oneline -1
b73a8c1 定休日を追加
 hours.txt | 1 +
 1 file changed, 1 insertion(+)

diff --git a/hours.txt b/hours.txt
index e89129b..c1f5f87 100644
--- a/hours.txt
+++ b/hours.txt
@@ -1 +1,2 @@
 営業時間 11:00-18:00
+定休日 水曜日

For listing only the changed files, see my article on using git diff to show only the list of changed files.

Reducing the unchanged context lines

Each diff hunk includes three unchanged lines before and after each change as context. According to the official description of -U<n> in the git log reference, you can change this count by writing a number after -U. -U0 shows only the changed lines. In the diff for menu.txt in 076169c, the 紅茶 400円 line no longer appeared.

console
$ git log -p -U0 --oneline -1 -- menu.txt
076169c 値上げと営業時間
diff --git a/menu.txt b/menu.txt
index c02f2d8..4aa77e8 100644
--- a/menu.txt
+++ b/menu.txt
@@ -1 +1 @@
-コーヒー 400円
+コーヒー 450円

The same description says that giving -U also turns on -p. I removed -p and ran git log -U0 --oneline -1 076169c -- menu.txt, which shows one commit from 076169c, and it printed the same diff.

Merge commits show no diff with -p alone

I added cake on the branch feature/cake, fixed the README on main, and then merged with --no-ff.

console
$ git log --oneline --graph
*   0bfcbba ケーキのブランチをマージ
|\  
| * fcb6876 ケーキを追加
* | 72a9bb7 休みの案内を追加
|/  
* b73a8c1 定休日を追加
* 076169c 値上げと営業時間
* d6937c0 メニューを追加
* ac6f1a6 最初のコミット

With git log -p, the merge commit 0bfcbba shows no diff at all.

console
$ git log -p --oneline -3
0bfcbba ケーキのブランチをマージ
72a9bb7 休みの案内を追加
diff --git a/README.md b/README.md
index c28e4e4..2f3a096 100644
--- a/README.md
+++ b/README.md
@@ -1 +1,3 @@
 # Cat cafe memo
+
+水曜日はお休みです。
fcb6876 ケーキを追加
diff --git a/menu.txt b/menu.txt
index 4aa77e8..2c39503 100644
--- a/menu.txt
+++ b/menu.txt
@@ -1,2 +1,3 @@
 コーヒー 450円
 紅茶 400円
+ケーキ 500円

According to the git log official reference, unless you specify --diff-merges or a similar option, -p does not show diffs for merge commits. However, when you use --first-parent, the default is to show the diff against the first parent. With --diff-merges=first-parent, the diff is taken against main before the merge (the first parent, 72a9bb7). The only change that the merge brought into main was the cake line in menu.txt.

console
$ git log -p --oneline --diff-merges=first-parent -1
0bfcbba ケーキのブランチをマージ
diff --git a/menu.txt b/menu.txt
index 4aa77e8..2c39503 100644
--- a/menu.txt
+++ b/menu.txt
@@ -1,2 +1,3 @@
 コーヒー 450円
 紅茶 400円
+ケーキ 500円

For showing only merge commits, or limiting the output to merges into main with --first-parent, see my article on using git log --merges to show only merge commits.

​Posted by 86