Retrieving notes from the remote repository

So far, we have been creating notes in our own local repository, which is okay. But if we want to share those notes, we have to be sure to be able to push them. We would also like to be able to retrieve other people's notes from the remote repository. Unfortunately, this is not so plain and simple.

Getting ready

Before we can start, we need another clone from the local clone we already have. This is to show the push and fetch mechanism of Git with git notes:

  1. Start by checking out the master branch:
    $ git checkout master
    Checking out files: 100% (1529/1529), done.
    Switched to branch 'master'
    Your branch is up-to-date with 'origin/master'.
    
  2. Now, create local branches of all the stable-3.x branches:
    $ git branch stable-3.0 origin/stable-3.0
    Branch stable-3.0 set up to track remote branch stable-3.0 from origin.
    $ git branch stable-3.1 origin/stable-3.1
    Branch stable-3.1 set up to track remote branch stable-3.1 from origin.
    $ git branch stable-3.2 origin/stable-3.2
    Branch stable-3.2 set up to track remote branch stable-3.2 from origin.
    
  3. We are checking out all these branches because we want to clone this repository and by default all the refs/heads/* branches will be cloned. So, when we clone the chapter5 directory, you will see that we only get the branches we see if you execute git branch:
    $ git branch
    * master
      myNotes
      notesMessage
      notesReference
      stable-3.0
      stable-3.1
      stable-3.2
    
  4. Now, go one directory up so you can create your new clone from the chapter5 directory:
    cd ..
    $ git clone ./chapter5 shareNotes
    Cloning into 'shareNotes'...
    done.
    
  5. Now, enter the shareNotes directory and run git branch -a to see that the only remote branches we have are the branches we had checked out as local branches in the chapter5 directory. After this, we are ready to fetch some notes:
    cd shareNotes
    $ git branch -a
    * master
      remotes/origin/HEAD -> origin/master
      remotes/origin/master
      remotes/origin/myNotes
      remotes/origin/notesMessage
      remotes/origin/notesReference
      remotes/origin/stable-3.0
      remotes/origin/stable-3.1
      remotes/origin/stable-3.2
    
  6. As predicted, the list matches the Git branch output from the chapter5 directory.

How to do it...

We have now prepared the setup to push and fetch notes. The challenge is that Git is not a default setup to retrieve and push notes, so you won't usually see other people's notes:

  1. We start by showing that we did not receive the notes during the clone:
    $ git log -1 b4f07df357fccdff891df2a4fa5c5bd9e83b4a4a --notes=alsoCherryPick
    warning: notes ref refs/notes/alsoCherryPick is invalid
    commit b4f07df357fccdff891df2a4fa5c5bd9e83b4a4a
    Author: Matthias Sohn <[email protected]>
    Date:   Tue Sep 24 09:11:47 2013 +0200
    
        Prepare re-signing pgm's ueberjar to avoid SecurityException
    
  2. As expected, the output does not show the note. In the chapter5 directory, we will see the note. To enable the notes to be fetched, we need to create a new fetch rule configuration; it needs to be similar to the fetch rule for refs/heads. Take a look at the configuration from git config:
    $ git config --get remote.origin.fetch
    +refs/heads/*:refs/remotes/origin/* 
    
  3. This shows that we are fetching refs/heads into the refs/remotes/origin reference, but what we also want to do is fetch refs/notes/* into refs/notes/*:
    $ git config --add remote.origin.fetch '+refs/notes/*:refs/notes/*'
    
  4. You should now have it configured. If you leave out the --add option from your command, you will overwrite your current settings. Verify that the rule now exists:
    $ git config --get-all  remote.origin.fetch
    +refs/heads/*:refs/remotes/origin/*
    +refs/notes/*:refs/notes/*
    
  5. Now, try and fetch the notes:
    $ git fetch
    From c:/Users/Rasmus/repos/./chapter5
     * branch            master     -> FETCH_HEAD
     * [new ref]         refs/notes/alsoCherryPick -> refs/notes/alsoCherryPick
     * [new ref]         refs/notes/commits -> refs/notes/commits
     * [new ref]         refs/notes/defect -> refs/notes/defect
     * [new ref]         refs/notes/defects -> refs/notes/defects
    
  6. As the Git output indicates, we have received some new refs. So, let us check whether we have the note on the commit now:
    $ git log -1 b4f07df357fccdff891df2a4fa5c5bd9e83b4a4a --notes=alsoCherryPick
    commit b4f07df357fccdff891df2a4fa5c5bd9e83b4a4a
    Author: Matthias Sohn <[email protected]>
    Date:   Tue Sep 24 09:11:47 2013 +0200
    
        Prepare re-signing pgm's ueberjar to avoid SecurityException
    More output...
        Signed-off-by: Matthias Sohn <[email protected]>
    Notes (alsoCherryPick):
        570bba5
    
  7. We now have the notes in our repository, which is what we expected.

How it works...

We fetched the notes. The reason why it works is because of the way we fetched. By default, Git is configured to fetch refs/heads/* into refs/remotes/origin/*. This way, we can easily keep track of what is remote and what is local. The branches in our local repo are in refs/heads/*. These branches are also listed when you execute git branch.

For notes, we need to fetch refs/notes/* into refs/notes/* since we want to get the notes from the server and use them with the git show, git log, and git notes Git commands.

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

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