git notes: add information to commits without rewriting history
A commit is immutable: changing its message changes its hash and rewrites the history after it, which is not acceptable on a shared branch. Sometimes information arrives later: the commit was deployed to production at 14:22, the reviewer approved it, a test on PHP 8.3 failed. git notes attaches such text to an existing commit without touching the commit itself.
Problem: A team wants to know which commit of a WordPress theme is on production and when it was deployed. Writing that into commit messages is impossible after the fact, and a separate spreadsheet is always out of date.
Solution: Let the deploy script add a note to the deployed commit in a separate notes ref (refs/notes/deploys), push and fetch the notes refs explicitly, because git does not transfer them by default, and show them in git log with --notes.
The basics
git notes add -m "Reviewed by Anna, OK for release" 3f2a9c1 # a note on a commit git notes show 3f2a9c1 # read it git notes append -m "Tested on PHP 8.3" 3f2a9c1 # add a line git notes edit 3f2a9c1 # open in the editor git notes remove 3f2a9c1 git log --notes -3 # log with notes
Without a commit, the commands use HEAD. A commit can have one note per notes ref; add on a commit that already has a note fails, use append or add -f to replace it. Internally a note is a blob in a separate branch-like ref, refs/notes/commits by default, so the commit hash does not change.
Separate refs for different kinds of notes
git notes --ref=deploys add -m "production 2025-01-05 14:22 UTC, release 2.4.0" HEAD git notes --ref=ci add -m "phpunit: 212 passed; phpstan: 0 errors" HEAD git log --notes=deploys --oneline -5 git log --notes='*' -5 # all notes refs
--ref=deploys is short for refs/notes/deploys. Separate refs keep the deploy history apart from review comments and can be shared or ignored independently.
Push and fetch
Notes are not pushed or fetched with branches:
git push origin 'refs/notes/*' git fetch origin 'refs/notes/*:refs/notes/*'
To fetch them automatically with every git fetch:
git config --add remote.origin.fetch '+refs/notes/*:refs/notes/*'
If two people add notes to the same ref at the same time, the second push is rejected like any non-fast-forward push. git notes merge exists for that; for automated notes from a deploy script, it is simpler when only the script writes to its ref.
In a deploy script
#!/usr/bin/env bash
set -euo pipefail
commit=$(git rev-parse HEAD)
# ... deployment steps ...
git fetch origin 'refs/notes/deploys:refs/notes/deploys' || true
git notes --ref=deploys append -m "production $(date -u '+%F %H:%M') UTC by ${USER}" "$commit"
git push origin refs/notes/deploys
append instead of add means that a redeploy of the same commit adds a second line instead of failing. Afterwards git log --notes=deploys shows on every deployed commit when it went live. The same works in a CI pipeline, as a last step after a successful deployment: GitHub Actions CI/CD for WordPress plugins.
Limits
- Hosting interfaces show notes poorly or not at all: GitHub stopped displaying them years ago. Notes are for people and scripts that work with the repository itself.
- A rebase creates new commits, and the notes stay on the old ones unless
notes.rewriteRefis configured (git config notes.rewriteRef refs/notes/commits). On branches that are rebased often, notes get lost. - Notes are not signed and can be changed by anyone with push access, so they are a log, not an audit trail.
NOTE: For information that belongs to the change itself (why it was made, which ticket), the commit message is still the right place, written before the commit is shared. Cleaning up messages before pushing: interactive rebase.