We will add some extra information to the already released code. If we were doing it in the actual commits, we would see the commit hashes change.
Before we start, we need a repository to work in; you can use the previous clone of jgit
, but to get an output from the example that's almost identical, you can clone the jgit
repository as follows:
$ git clone https://git.eclipse.org/r/jgit/jgit chapter5 $ cd chapter5
We start by creating a local branch notesMessage
tracking origin/stable-3.2
. Then, we will try and change the commit message and see that the commit hash changes:
notesMessage
tracking origin/stable-3.2
:$ git checkout -b notesMessage --track origin/stable-3.2 Branch notesMessage set up to track remote branch stable-3.2 from origin. Switched to a new branch 'notesMessage'
HEAD
of your branch:$ git log -1 commit f839d383e6fbbda26729db7fd57fc917fa47db44 Author: Matthias Sohn <[email protected]> Date: Wed Dec 18 21:16:13 2013 +0100 Prepare post 3.2.0 builds Change-Id: Ie2bfdee0c492e3d61d92acb04c5bef641f5f132f Signed-off-by: Matthias Sohn <[email protected]>
git commit --amend
. Add a line above the Change-Id:
line with "Update MANIFEST files"
:$ git commit --amend
$ git log -1 commit 5ccc9c90d29badb1bd860d29860715e0becd3d7b Author: Matthias Sohn <[email protected]> Date: Wed Dec 18 21:16:13 2013 +0100 Prepare post 3.2.0 builds Update MANIFEST files Change-Id: Ie2bfdee0c492e3d61d92acb04c5bef641f5f132f Signed-off-by: Matthias Sohn [email protected]
f839d383e6fbbda26729db7fd57fc917fa47db44
to 5ccc9c90d29badb1bd860d29860715e0becd3d7b
, as the commit is derived from the content in the commit, the parents of the commit, and the commit message. So, the commit hash will change when updating the commit message. Since we have changed the content of the HEAD
commit, we are no longer based on the HEAD
commit of the origin/stable-3.2
branch. This becomes visible in gitk
and git status
:$ git status On branch notesMessage Your branch and 'origin/stable-3.2' have diverged, and have 1 and 1 different commit each, respectively. (use "git pull" to merge the remote branch into yours) nothing to commit, working directory clean
origin/stable-3.2
; this is also visible from Gitk. Notice that I specified which branches and commits I want to see with gitk
. In this case, I want to see origin/stable-3.2
and HEAD
:$ gitk origin/stable-3.2 HEAD
origin/stable-3.2
and then adding a note to the commit:$ git reset --hard origin/stable-3.2 HEAD is now at f839d38 Prepare post 3.2.0 builds
$ git notes add -m "Update MANIFEST files"
-m
flag and then a message. The log will now be visible when running git log
:$ git log -1 commit f839d383e6fbbda26729db7fd57fc917fa47db44 Author: Matthias Sohn <[email protected]> Date: Wed Dec 18 21:16:13 2013 +0100 Prepare post 3.2.0 builds Change-Id: Ie2bfdee0c492e3d61d92acb04c5bef641f5f132f Signed-off-by: Matthias Sohn <[email protected]> Notes: Update MANIFEST files
Notes:
section with our note. Although it does not add the note directly in the commit message as the --amend
option does, we still have our important addition to the commit message. We can verify with git status
that we have no longer diverged:$ git status On branch notesMessage Your branch is up-to-date with 'origin/stable-3.2'. nothing to commit, working directory clean
So, you have your notes for your commit and now you want to add to them. You will perhaps expect that you just add the note again with more information. This is not the case. You have the option to append, edit, or force the note to be created:
$ git notes add -m "Update MANIFESTS files for next version" error: Cannot add notes. Found existing notes for object f839d383e6fbbda26729db7 fd57fc917fa47db44. Use '-f' to overwrite existing notes
-f
flag:$ git notes add -f -m "Update MANIFESTS files for next version" Overwriting existing notes for object f839d383e6fbbda26729db7fd57fc917fa47db44
-f
flag. You can also use --force
, which is the same. Verify it with git log
:$ git log -1 commit f839d383e6fbbda26729db7fd57fc917fa47db44 Author: Matthias Sohn <[email protected]> Date: Wed Dec 18 21:16:13 2013 +0100 Prepare post 3.2.0 builds Change-Id: Ie2bfdee0c492e3d61d92acb04c5bef641f5f132f Signed-off-by: Matthias Sohn <[email protected]> Notes: Update MANIFESTS files for next version
git notes append
:$ git notes append -m "Verified by Rasmus Voss"
git log
again. To keep the output to a minimum, we are using --oneline
. This will show a minimum output of the commit. But to show the note, we have to add --notes
, which will show the notes for the commits in the output:$ git log -1 --notes --oneline f839d38 Prepare post 3.2.0 builds Notes: Update MANIFESTS files for next version Verified by Rasmus Voss
edit
option, you will see that you can only use this with the -m
flag. This makes good sense as you should edit the note and not overwrite or append an already created note:$ git notes edit -m "Rasmus Voss" The -m/-F/-c/-C options have been deprecated for the 'edit' subcommand. Please use 'git notes add -f -m/-F/-c/-C' instead.