Make the Most of git notes to Add Extra Information to Commits
git notes
is an often-overlooked Git feature that lets you add annotations to a commit without changing its hash, keeping your codebase clean and history intact. It’s perfect for adding comments, feedback, or extra details directly into your commit history. You can check out the official documentation here: https://git-scm.com/docs/git-notes
How git notes Works
With git notes, you can attach notes to existing commits. Let’s say you received feedback on a function that needs optimization. Instead of digging through messages and review comments, you can do this:
- Create a commit as usual:
git commit -m "Implement search function"
- Add a note to the commit, such as optimization feedback:
git notes add -m "Feedback from Luca: optimize the query for large datasets" <commit-hash>
- To view the commit history with notes, use:
git log --notes
Organizing Notes
For structured teamwork, git notes
allows multiple notes with distinct references, like "QA," "Security," or "Performance."
For example:
git notes --ref=QA add -m "QA: add tests for edge cases" <commit-hash>
git log --notes=QA
Imagine working on an open-source project where you want pull request discussions preserved within merge commits for future access, even offline. Using git notes
, all review context stays available without changing the commit hash.
Syncing Notes with Remote
Be aware that notes aren’t synced automatically, so you’ll need to specify the reference when pushing:
git push origin refs/notes/*:refs/notes/*