72

it seems like there is no proper documentation regarding git notes. I have added some notes to one of the commit using git notes add command. but when i push the commit, and later do a separate clone, i dont see the note message there. Is there a way to push all the note messages added via git notes command?

Iowa
  • 2,171
  • 4
  • 22
  • 31

1 Answers1

82

Push all notes:

git push <remote> 'refs/notes/*'

Fetch all notes:

git fetch origin 'refs/notes/*:refs/notes/*'

[[git-scm.org] (archive)]

A word of warning: do not use git pull in place of git fetch (that is, git pull origin refs/notes/*:refs/notes/* is wrong). The overall details are complex, but the particular reason git pull is wrong here is that you do not want to merge or rebase refs/notes/commits with your current branch.

If you encounter zsh: no matches found: refs/notes/* error, consider quoting the argument: git push origin 'refs/notes/*'

l0st3d
  • 2,860
  • 1
  • 27
  • 29
simont
  • 68,704
  • 18
  • 117
  • 136
  • when i do a git pull, it does a merge and then Merge commit 'refs/notes/commits' and then creates a new commit.. why is that so? – Iowa Aug 16 '13 at 09:10
  • 1
    @user2663585 From `man git-pull`: "git-pull - Fetch from and merge with another repository or a branch". `git pull` does both `git fetch` and `git merge`, so if the remote you pull from has commits you don't, you'll do a merge (which makes a new commit). To avoid this, if you want to examine what you'll merge with (instead of doing it all in one), you can run `git fetch`, then `git merge` yourself. – simont Aug 16 '13 at 09:38
  • 1
    Here is what i did, i created a test file, added, committed and pushed it.. then i created a note for that commit and did a note push using "git push origin refs/notes/*".. after that i did a "git checkout master" and did a "git pull origin refs/notes/*:refs/notes/*".. Even though my local repo was in sync with remote central repo, it created a merge commit.. and when i did a git log, it showed 2 more entries, one saying "Notes added by git nodes add" and the 2nd one says "Merge commit 'refs/notes/commits'".. Does this mean, that the git notes internaly does a commit??? pls help me undestand – Iowa Aug 16 '13 at 10:49
  • 8
    @user2663585: Yes, notes are commits, but *not on the branch they point to*. The problem is that `git pull origin blah` means merge `blah` into **your current branch**, even when blah is `refs/notes/commits`. So you merged the notes into your main branch, instead of leaving them off to the side where they belong. – torek Aug 16 '13 at 11:31
  • Don't use that fetch command, remember "You never do your own development on branches that appear on the right hand side of a "? It applies here as well, if you already have other notes in the local repository at first it won't work, and when you'll likely add a '+' before sure that this way they'll be merged, you'll lose all your local notes (well no, they just get pushed a little into the reflog, if you enabled it, no big deal!! ). – gbr Oct 21 '15 at 11:56
  • 2
    The only way to have this feature somewhat "working" is to put them in another 'namespace' (where the f*ck are these namespaces defined, by the way?) _inside_ refs/notes, for example in refs/notes/origin/ (otherwise `git notes merge` won't do anything - not even error out). So use `git fetch origin refs/notes/*:refs/notes/origin/*` - and then merge them with your local notes. – gbr Oct 21 '15 at 11:56
  • 25
    To always get notes when pulling one can add `fetch = +refs/notes/*:refs/notes/*` to the remote origin section in the git config. – Zitrax Apr 03 '16 at 19:06
  • 7
    To always *push* notes add `push = +refs/notes/*:refs/notes/*` to the remote origin section in the Git config. – Kenny Evitt May 11 '18 at 19:55
  • 4
    @Zitrax that causes git to force-update your local notes, thus overwriting any locally added notes whenever you `git fetch`. (Can of course recover with `git reflog notes/commits`) – derobert Feb 12 '19 at 21:15
  • 5
    If you encounter `zsh: no matches found: refs/notes/*` error, consider quoting the argument: `git push origin 'refs/notes/*'` – Slava Semushin Jan 16 '20 at 20:05
  • @torek it looks like your comment (plus the one from Zitrax) resolves a lot of the confusion here in the comments. I think your contributions would be better presented in the answer (and for the most part once an edit is complete to include them, these comments can basically all be flagged as no longer needed). I wanted to edit your comment + Zitrax's into the answer, but I lack the understanding to do so without potentially damaging the answer/your comment. If you disagree with the need for such an edit that is fine, but I thought I'd draw your attention here just in case. – Kraigolas Nov 04 '22 at 02:22
  • 1
    @Kraigolas: I'm not willing to edit this particular answer, but given time, I could construct my own answer. Because of the way notes are implemented, it's tricky to cover all cases. Some particularly easy cases aren't so bad, but detailing what works under what conditions gets very hairy. I can put in a warning not to use `git pull` though. – torek Nov 05 '22 at 07:56
  • @SlavaSemushin THANK YOU! I wish I could upvote your comment – Wayne F. Kaskie Mar 02 '23 at 00:27