Adding your first Git note

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.

Getting ready

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

How to do it…

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:

  1. Checkout the branch 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'
    
  2. List the commit hash of 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]>
    
  3. Change the commit message by amending the commit using git commit --amend. Add a line above the Change-Id: line with "Update MANIFEST files":
    $ git commit --amend
    
  4. Now, we list the commit again and see that the commit hash has changed:
    $ 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]
    
  5. Notice that the commit parts have changed from 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
    
  6. As you can see from the output, our branch has diverged from 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
    
    How to do it…
  7. To prevent the this result, we can add a note to the commit message. Let's start by resetting the branch to 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
    
  8. Now, add the same message as the previous one but just as a note:
    $ git notes add -m "Update MANIFEST files"
    
  9. We have added the note directly from the command line without invoking the editor by using the -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
    
  10. As you can see from the log output, we have a 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
    

There's more...

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:

  1. Start by trying to add the note again with additional information:
    $ 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
    
  2. As predicted, we cannot add the note but we can do it with the -f flag:
    $ git notes add -f -m "Update MANIFESTS files for next version"
    Overwriting existing notes for object f839d383e6fbbda26729db7fd57fc917fa47db44
    
  3. Git overwrites the existing notes due to the -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
    
  4. You can also append a current note with git notes append:
    $ git notes append -m "Verified by Rasmus Voss"
    
  5. There is no output from this unless something goes wrong, but you can verify this by using 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
    
  6. As we can see from the output, we have the line appended to the note. If you try to use the 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.
    
  7. As you can see, Git rejects doing and mentions other ways of doing it.

    Tip

    The git notes add and git notes edit commands without any arguments will do exactly the same, that is, open the configured editor and allow you to write a note to the commit.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset