git notesで、コミットにあとからメモを付ける方法

​​Git​コマンドライン

git notes add -m "メモ" <コミット> と打つと、コミットそのものを変えずに、あとからメモを付けられます。コミットを作り直さないので、ハッシュは変わりません。本番に反映した日や不具合の報告先など、コミットしたときには分からなかった情報を、コミットの履歴に添えて残したいときに使うコマンドです。

補足

検証環境は Ubuntu 24.04、Git 2.43.0 です。作者とコミッターの名前とメールアドレスは、例示用の値(Neko <neko@example.com>)です。日時は環境変数で決め、ページャーは使わない設定にしています。メモを付けるときは、どれも -m でメッセージを渡しました。エディターで書く操作(git notes edit など)、rebase でのメモの引き継ぎ、メモどうしのマージで食い違いが出る場合、GitHub などのサービスでの表示は試していません。

メモを付ける前の履歴

app という、3件のコミットがあるリポジトリで試しました。メモを付ける前の git log は、次のとおりです。git notes list は、メモが1つもないので何も出ません。

console
$ git log --oneline
5a2ee51 トップ画面を追加
a311519 ログイン画面を追加
e563966 最初のコミット

$ git log -1 HEAD~1
commit a3115192c799fb8661d6d19f0fa0acd511b7c69d
Author: Neko <neko@example.com>
Date:   Mon Nov 2 10:00:00 2026 +0900

    ログイン画面を追加

$ git notes list

git notes add でメモを付ける

2件目のコミット(HEAD~1)に、メモを付けます。コミットを指定しなければ、HEAD に付きます。

console
$ git notes add -m "本番には11月5日に反映済み" HEAD~1

$ git log -1 HEAD~1
commit a3115192c799fb8661d6d19f0fa0acd511b7c69d
Author: Neko <neko@example.com>
Date:   Mon Nov 2 10:00:00 2026 +0900

    ログイン画面を追加

Notes:
    本番には11月5日に反映済み

コミットメッセージの下に Notes: の行が増え、その下にメモが字下げされて出ました。コミットのハッシュ(a311519…)は、付ける前と同じです。git log --oneline で並べても、3件ともハッシュは変わっていません。

console
$ git log --oneline
5a2ee51 トップ画面を追加
a311519 ログイン画面を追加
e563966 最初のコミット

メモの中身だけを見るときは git notes show、メモが付いているコミットの一覧は git notes list で出せます。list の各行は、左がメモの中身を入れたオブジェクト、右がメモの付いたコミットです。

console
$ git notes show HEAD~1
本番には11月5日に反映済み

$ git notes list
08a2bc6bccb777e775810b7306a05b882bad977c a3115192c799fb8661d6d19f0fa0acd511b7c69d

git notes のリファレンスでも、メモの使いみちとして、コミットそのものを変えずにコミットメッセージを補うことが挙げられています。

書き足すときは append、書き換えるときは -f

メモがすでにあるコミットに、もう一度 git notes add をすると、エラーで止まりました。前のメモは残ったままです。

console
$ git notes add -m "別のメモ" HEAD~1
error: Cannot add notes. Found existing notes for object a3115192c799fb8661d6d19f0fa0acd511b7c69d. Use '-f' to overwrite existing notes

今のメモの後ろに書き足すときは、git notes append を使います。前のメモとの間には、空の行が1行入りました。

console
$ git notes append -m "不具合の報告: https://example.com/issues/12" HEAD~1

$ git notes show HEAD~1
本番には11月5日に反映済み

不具合の報告: https://example.com/issues/12

メモを丸ごと書き換えるときは、エラーの文にあるとおり -f を付けて add します。書き足した2つ目の段落も含めて、新しいメモに置き換わりました。

console
$ git notes add -f -m "本番には11月6日に反映済み(5日は取り消し)" HEAD~1
Overwriting existing notes for object a3115192c799fb8661d6d19f0fa0acd511b7c69d

$ git notes show HEAD~1
本番には11月6日に反映済み(5日は取り消し)

git log でメモを出す・出さない

ふつうの git log と git show では、オプションを付けなくてもメモが出ます。git show --stat では、メモは変更したファイルの一覧より上に出ました。

console
$ git show --stat HEAD~1
commit a3115192c799fb8661d6d19f0fa0acd511b7c69d
Author: Neko <neko@example.com>
Date:   Mon Nov 2 10:00:00 2026 +0900

    ログイン画面を追加

Notes:
    本番には11月6日に反映済み(5日は取り消し)

 login.txt | 1 +
 1 file changed, 1 insertion(+)

一方で、メモを付けた直後の git log --oneline には、メモが出ていませんでした。git log のリファレンスにある --notes の説明にも、メモを出すのが標準になるのは --pretty・--format・--oneline を付けていないとき、と書かれています。--oneline でもメモを見たいときは、--notes を足します。

console
$ git log --oneline --notes
5a2ee51 トップ画面を追加
a311519 ログイン画面を追加
Notes:
    本番には11月6日に反映済み(5日は取り消し)

e563966 最初のコミット

--format で書式を決めるときは、%N がメモに置き換わります。この出力では、メモのあるコミットの後ろに空の行が1行入りました。

console
$ git log --format="%h %s / %N" -3
5a2ee51 トップ画面を追加 / 
a311519 ログイン画面を追加 / 本番には11月6日に反映済み(5日は取り消し)

e563966 最初のコミット / 

反対に、ふつうの git log でメモを隠したいときは、--no-notes を付けます。

console
$ git log -1 --no-notes HEAD~1
commit a3115192c799fb8661d6d19f0fa0acd511b7c69d
Author: Neko <neko@example.com>
Date:   Mon Nov 2 10:00:00 2026 +0900

    ログイン画面を追加

メモは refs/notes/commits という別の ref に入る

メモは、コミットの中ではなく、refs/notes/commits という別の ref にしまわれます。メモを変えるたびに、この ref に新しいコミットが1つ増えます。ここまでに add・append・add -f の3回を実行したので、3件のコミットがありました。作者には、ふつうのコミットと同じく git config の user.name が使われます。

console
$ git log --format="%h %an %ad %s" --date=iso refs/notes/commits
c7a1d0f Neko 2026-11-10 10:10:00 +0900 Notes added by 'git notes add'
5a3e3f9 Neko 2026-11-10 10:05:00 +0900 Notes added by 'git notes append'
904d75e Neko 2026-11-10 10:00:00 +0900 Notes added by 'git notes add'

この ref のツリーを見ると、メモの付いたコミットのハッシュを名前にしたファイルが1つ入っています。

console
$ git ls-tree refs/notes/commits
100644 blob 77441ebdafabb021867c0743982e8a6c8edb3616	a3115192c799fb8661d6d19f0fa0acd511b7c69d

用途ごとにメモを分ける(–ref)

--ref を付けると、refs/notes/commits とは別の ref にメモを付けられます。--ref=review と書くと、refs/notes/review になります。最新のコミットに、レビューのメモを付けました。

console
$ git notes --ref=review add -m "レビュー: 確認済み" HEAD

$ git log -1 HEAD
commit 5a2ee51edf5132a4cd101f2fd219c5e291cb8a80
Author: Neko <neko@example.com>
Date:   Tue Nov 3 10:00:00 2026 +0900

    トップ画面を追加

何も付けない git log に出るのは、refs/notes/commits のメモだけです。review のメモは出ませんでした。見るときは --notes=review を付けます。見出しは Notes (review): になり、どの ref のメモかが分かります。

console
$ git log -1 --notes=review HEAD
commit 5a2ee51edf5132a4cd101f2fd219c5e291cb8a80
Author: Neko <neko@example.com>
Date:   Tue Nov 3 10:00:00 2026 +0900

    トップ画面を追加

Notes (review):
    レビュー: 確認済み

--notes="*" とすると、refs/notes/ の下にある2つの ref のメモを、どちらも出せます。最新の2件を出すと、1件目には review のメモ、2件目には refs/notes/commits のメモが出ました。

console
$ git log -2 --notes="*"
commit 5a2ee51edf5132a4cd101f2fd219c5e291cb8a80
Author: Neko <neko@example.com>
Date:   Tue Nov 3 10:00:00 2026 +0900

    トップ画面を追加

Notes (review):
    レビュー: 確認済み

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

    ログイン画面を追加

Notes:
    本番には11月6日に反映済み(5日は取り消し)

$ git for-each-ref refs/notes
c7a1d0f59e1ce1f4204efe9281fa7c9f025cd367 commit	refs/notes/commits
ea65a0bd80525fdab5b70e27d6c0737d16bef9b6 commit	refs/notes/review

メモを消す(remove)

メモを消すときは git notes remove です。--ref を付ければ、その ref のメモを消します。消したあとの list には何も出ませんでした。

console
$ git notes --ref=review remove HEAD
Removing note for object HEAD

$ git notes --ref=review list

amend でコミットを作り直すと、メモは付いてこない

メモは、コミットのハッシュに結び付いています。git commit --amend でコミットを作り直すと、ハッシュが変わります。最新のコミットにメモを付けてから amend したところ、新しいコミットの git log には、メモが出ませんでした。

console
$ git notes add -m "あとで文言を見直す" HEAD

$ git commit -q --amend -m "トップ画面を追加する"

$ git log -1 HEAD
commit 2bc1d2bc0e25e0b8e83c8c8bbb138a4dc4c1f53f
Author: Neko <neko@example.com>
Date:   Tue Nov 3 10:00:00 2026 +0900

    トップ画面を追加する

$ git notes list
d9f667026a9b927fd18c19a547b95de584354756 5a2ee51edf5132a4cd101f2fd219c5e291cb8a80
77441ebdafabb021867c0743982e8a6c8edb3616 a3115192c799fb8661d6d19f0fa0acd511b7c69d

$ git config notes.rewriteRef

git notes list を見ると、メモは作り直す前のコミット(5a2ee51)に付いたままです。git config のリファレンスにある notes.rewriteRef の説明によると、この設定には初期値がなく、設定しないと amend や rebase のときにメモが写されません。refs/notes/commits を設定すると、--ref を付けずに付けたメモ(refs/notes/commits のメモ)が写されるようになります。

設定してから、もう一度メモを付けて amend しました。今度は、新しいコミット(f653a90)にもメモが出ました。

console
$ git config notes.rewriteRef refs/notes/commits

$ git notes add -m "あとで文言を見直す" HEAD

$ git commit -q --amend -m "トップ画面を追加した"

$ git log -1 HEAD
commit f653a909241cdf17243876576d351dbdfdb8fe3d
Author: Neko <neko@example.com>
Date:   Tue Nov 3 10:00:00 2026 +0900

    トップ画面を追加した

Notes:
    あとで文言を見直す

$ git notes list
d9f667026a9b927fd18c19a547b95de584354756 2bc1d2bc0e25e0b8e83c8c8bbb138a4dc4c1f53f
d9f667026a9b927fd18c19a547b95de584354756 5a2ee51edf5132a4cd101f2fd219c5e291cb8a80
77441ebdafabb021867c0743982e8a6c8edb3616 a3115192c799fb8661d6d19f0fa0acd511b7c69d
d9f667026a9b927fd18c19a547b95de584354756 f653a909241cdf17243876576d351dbdfdb8fe3d

メモは移るのではなく、写されます。作り直す前のコミット(2bc1d2b)にも、同じメモが残っていました。

メモを push して共有する

メモは別の ref にあるので、ブランチを push しただけでは送られません。共有用のリポジトリ(remote.git)を作り、ブランチとは別に refs/notes/commits を push しました。

console
$ git init -q --bare remote.git

$ git remote add origin ../remote.git

$ git push -q origin main

$ git push origin refs/notes/commits
To ../remote.git
 * [new reference]   refs/notes/commits -> refs/notes/commits

受け取る側も同じで、clone しただけでは、メモは届きません。clone したリポジトリでは、メモを付けたコミットの git log に Notes: が出ず、git notes list も空でした。

console
$ git clone -q remote.git copy

$ git log -1 HEAD~1
commit a3115192c799fb8661d6d19f0fa0acd511b7c69d
Author: Neko <neko@example.com>
Date:   Mon Nov 2 10:00:00 2026 +0900

    ログイン画面を追加

$ git notes list

clone したリポジトリで、fetch のときに取ってくる ref の指定(remote.origin.fetch)に refs/notes/* を足してから fetch すると、メモが届きました。この設定はリポジトリに残るので、次からは git fetch だけでメモも取ってきます。

console
$ git config --add remote.origin.fetch "+refs/notes/*:refs/notes/*"

$ git config --get-all remote.origin.fetch
+refs/heads/*:refs/remotes/origin/*
+refs/notes/*:refs/notes/*

$ git fetch -q

$ git for-each-ref refs/notes
ab6d9ad94e32f2629a3927d46c1ff26bdf89106b commit	refs/notes/commits

$ git log -1 HEAD~1
commit a3115192c799fb8661d6d19f0fa0acd511b7c69d
Author: Neko <neko@example.com>
Date:   Mon Nov 2 10:00:00 2026 +0900

    ログイン画面を追加

Notes:
    本番には11月6日に反映済み(5日は取り消し)

+ 付きの refspec では、手元だけのメモが fetch で消える

前の節で足した +refs/notes/*:refs/notes/* は、先頭に + が付いています。この設定のまま、手元で付けたまま、まだ push していないメモがあるときに fetch を試しました。

clone したリポジトリ(copy)で、1件目のコミット(HEAD~2)にメモを付けます。

console
$ git notes add -m "手元だけのメモ" HEAD~2

$ git notes show HEAD~2
手元だけのメモ

そのあと、元のリポジトリ(app)では別のコミットにメモを書き足し、refs/notes/commits を push しました。

console
$ git notes append -m "共有用に書き足したメモ" HEAD~1

$ git push -q origin refs/notes/commits

copy に戻って fetch すると、refs/notes/commits が (forced update) で置き換わりました。終了コードは 0 で、エラーは出ていません。書き足したメモは届きましたが、HEAD~2 に付けたメモはなくなりました。(From の行には作業用のフォルダのパスが出るので、grep -v で除いています。)

console
$ git fetch -v origin 2>&1 | grep -v "^From"; echo "exit=${PIPESTATUS[0]}"
 = [up to date]      main               -> origin/main
 + 01ad079...5696049 refs/notes/commits -> refs/notes/commits  (forced update)
exit=0

$ git notes show HEAD~2
error: no note found for object e5639665d087edf66b4a5c0d6760c5eec4b83069.

$ git notes show HEAD~1
本番には11月6日に反映済み(5日は取り消し)

共有用に書き足したメモ
注意

+refs/notes/*:refs/notes/* を fetch の設定に入れていると、共有用のリポジトリのメモと手元のメモが食い違ったとき、まだ push していない手元のメモは、エラーも出ずに消えます。手元のメモを残したいときは、次の節のように + を外し、git notes merge でまとめます。

+ を外すと fetch は拒否され、git notes merge でまとめられる

remote.origin.fetch のメモの行から + を外して、同じことをもう一度試しました。--replace-all の最後の "notes" は、置き換える行を選ぶ正規表現で、ここでは notes を含む行だけを置き換えます。

console
$ git config --replace-all remote.origin.fetch "refs/notes/*:refs/notes/*" "notes"

$ git config --get-all remote.origin.fetch
+refs/heads/*:refs/remotes/origin/*
refs/notes/*:refs/notes/*

$ git notes add -m "手元だけのメモ(2回目)" HEAD~2

app でも、もう一度メモを書き足して push します。

console
$ git notes append -m "共有用に書き足したメモ(2回目)" HEAD~1

$ git push -q origin refs/notes/commits

今度の fetch は (non-fast-forward) で拒否され、終了コードは 1 になりました。手元のメモは残っています。そのかわり、app で書き足した2回目のメモは届いていません。

console
$ git fetch -v origin 2>&1 | grep -v "^From"; echo "exit=${PIPESTATUS[0]}"
 = [up to date]      main               -> origin/main
 ! [rejected]        refs/notes/commits -> refs/notes/commits  (non-fast-forward)
exit=1

$ git notes show HEAD~2
手元だけのメモ(2回目)

$ git notes show HEAD~1
本番には11月6日に反映済み(5日は取り消し)

共有用に書き足したメモ

両方のメモを残すには、共有用のリポジトリのメモを別の名前の ref(ここでは refs/notes/origin-commits)に取ってきてから、git notes merge で手元の refs/notes/commits にまとめます。git notes のリファレンスによると、同じコミットのメモが両方で書き換えられて食い違ったときは、何も指定しなければ手で直す方式(manual)になります。この検証では、両方で書き換えたコミットがなかったので、何も聞かれずにまとまりました。

console
$ git fetch -q origin refs/notes/commits:refs/notes/origin-commits

$ git notes merge refs/notes/origin-commits

$ git notes show HEAD~2
手元だけのメモ(2回目)

$ git notes show HEAD~1
本番には11月6日に反映済み(5日は取り消し)

共有用に書き足したメモ

共有用に書き足したメモ(2回目)

まとめたあとの refs/notes/commits は、拒否されずに push できました。

console
$ git push -q origin refs/notes/commits

$ git ls-remote origin refs/notes/commits
bb7e7896fcba81e31516859e69e2a59d9276be2b	refs/notes/commits

​Posted by 86