Skip to content

Instantly share code, notes, and snippets.

@devlights
Created July 7, 2025 08:52
Show Gist options
  • Save devlights/c738ffb20ce15d86c6700346ffa82c12 to your computer and use it in GitHub Desktop.
Save devlights/c738ffb20ce15d86c6700346ffa82c12 to your computer and use it in GitHub Desktop.
git notesについてのメモ

git notes について

git notesは、Gitオブジェクト(主にコミット)に対してメタデータを後から追加できる機能。

コミット履歴を変更することなく、追加情報を関連付けることが出来る。

特徴

  • コミットハッシュを変更せずに情報を追加可能
  • 複数の名前空間(namespace)で管理可能
  • デフォルトではrefs/notes/commitsに格納
  • 通常のGitオブジェクトとして管理される

試してみた

# -----------------------------------------------------
# 適当なディレクトリを作成
# -----------------------------------------------------
$ mkdir app
$ cd app

# -----------------------------------------------------
# リポジトリを初期化
# -----------------------------------------------------
$ git init
Initialized empty Git repository in /home/dev/tmp/app/.git/
$ git config --local user.name helloworld
$ git config --local user.email [email protected]

# -----------------------------------------------------
# 何個かコミット作っておく
# -----------------------------------------------------
$ go mod init app
go: creating new go.mod: module app
$ touch main.go

$ git status
On branch main

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        go.mod
        main.go

nothing added to commit but untracked files present (use "git add" to track)

$ git add .
$ git commit -m "1st"
[main (root-commit) 2eb7911] 1st
 2 files changed, 5 insertions(+)
 create mode 100644 go.mod
 create mode 100644 main.go

# -----------------------------------------------------
# ログ確認
# -----------------------------------------------------
$ git log -n 1
commit 2eb7911d8b2a410307792b1a1cce11490f009a26 (HEAD -> main)
Author: helloworld <[email protected]>
Date:   Mon Jul 7 17:27:51 2025 +0900

    1st

# -----------------------------------------------------
# notesを付けてみる (現在のコミットに付与)
# -----------------------------------------------------
$ git notes add -m "初回コミット完了。実装はまだない"

# -----------------------------------------------------
# notesを確認
# -----------------------------------------------------
$ git notes show
初回コミット完了。実装はまだない

# -----------------------------------------------------
# notesを編集 ($EDITORが使われる。指定なしの場合はnano)
# -----------------------------------------------------
$ git notes edit
$ git notes show
初回コミット完了。未実装。

# -----------------------------------------------------
# notesを削除
# -----------------------------------------------------
$ git notes remove
Removing note for object HEAD
$ git notes show
error: no note found for object 2eb7911d8b2a410307792b1a1cce11490f009a26.

# -----------------------------------------------------
# もう一回付け直し。今度はコミットハッシュを指定して付与
# -----------------------------------------------------
$ git log -n 1 --oneline
2eb7911 (HEAD -> main) 1st
$ git notes add -m "付け直し" 2eb7911
$ git notes show
付け直し

# -----------------------------------------------------
# git logで見る方が見やすい (--show-notesオプション付与)
# -----------------------------------------------------
$ git log -n 1 --show-notes
commit 2eb7911d8b2a410307792b1a1cce11490f009a26 (HEAD -> main)
Author: helloworld <[email protected]>
Date:   Mon Jul 7 17:27:51 2025 +0900

    1st

Notes:
    付け直し

# -----------------------------------------------------
# 別の名前空間にnotesを保存
# (--refオプションの位置大事。addより前に指定しないと駄目)
# -----------------------------------------------------
$ git notes --ref=notes/my add -m "個人的なノート"

# -----------------------------------------------------
# デフォルトの名前空間 (notes/commits) 以外を見たい場合は 
# --show-ref='*' とするか名前空間を明示的に指定する
# -----------------------------------------------------
$ git log --show-ref='*'
commit 2eb7911d8b2a410307792b1a1cce11490f009a26 (HEAD -> main)
Author: helloworld <[email protected]>
Date:   Mon Jul 7 17:27:51 2025 +0900

    1st

Notes:
    付け直し

Notes (my):
    個人的なノート

# -----------------------------------------------------
# 特定のnotesが付与されているコミットを探す
# -----------------------------------------------------
$ git log --grep='個人' --show-notes   # デフォルト名前空間のnotesには、このキーワードは存在しないのでヒットしない
$ git log --grep='個人' --show-notes='*'
commit 2eb7911d8b2a410307792b1a1cce11490f009a26 (HEAD -> main)
Author: helloworld <[email protected]>
Date:   Mon Jul 7 17:27:51 2025 +0900

    1st

Notes:
    付け直し

Notes (my):
    個人的なノート
$ git log --grep='個人' --show-notes='my'    # 明示的な名前空間の指定
commit 2eb7911d8b2a410307792b1a1cce11490f009a26 (HEAD -> main)
Author: helloworld <[email protected]>
Date:   Mon Jul 7 17:27:51 2025 +0900

    1st

Notes:
    付け直し

Notes (my):
    個人的なノート

# -----------------------------------------------------
# どんな名前空間が存在するかを調べる
# -----------------------------------------------------
$ git for-each-ref --format='%(refname:short)' refs/notes/
notes/commits
notes/my
$ git show-ref | grep refs/notes/ | cut -d' ' -f2
refs/notes/commits
refs/notes/my

# -----------------------------------------------------
# このリポジトリ、もういらないので削除
# -----------------------------------------------------
$ cd ..
$ rm -rf app
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment