Thursday, February 3, 2011

How to tell which local branch is tracking which remote branch in Git?

I would like to know if there's a way to tell which local branch is tracking which remote branch in Git.

I'm using one remote server, which is named "origin".

  • Try git branch with options:

     -r
    List or delete (if used with -d) the remote-tracking branches. 
    
    -a
    List both remote-tracking branches and local branches. 
    

    Otherwise, examine your .git/config.

    PJ : They show branches, but I don't understand which one is tracking which.
    From Andrew
  • Using the example of my copy of Puppet checked out from the upstream Git repository on Github.com...

    $ git remote show origin
    * remote origin
      Fetch URL: git://github.com/reductivelabs/puppet.git
      Push  URL: git://github.com/reductivelabs/puppet.git
      HEAD branch: master
      Remote branches:
        0.24.x                 tracked
        0.25.x                 tracked
        2.6.x                  tracked
        master                 tracked
        next                   tracked
        primordial-ooze        tracked
        reins-on-a-horse       tracked
        testing                tracked
        testing-17-march       tracked
        testing-18-march       tracked
        testing-2-april        tracked
        testing-2-april-midday tracked
        testing-20-march       tracked
        testing-21-march       tracked
        testing-24-march       tracked
        testing-26-march       tracked
        testing-29-march       tracked
        testing-31-march       tracked
        testing-5-april        tracked
        testing-9-april        tracked
        testing4268            tracked
      Local branch configured for 'git pull':
        master merges with remote master
      Local ref configured for 'git push':
        master pushes to master (up to date)
    

    Then if I were to execute the following:

    $ git checkout -b local_2.6 -t origin/2.6.x 
    Branch local_2.6 set up to track remote branch 2.6.x from origin.
    Switched to a new branch 'local_2.6'
    

    And finally re-run the git remote show origin command again I will then see the following down near the bottom:

      Local branches configured for 'git pull':
        local_2.6 merges with remote 2.6.x
        master    merges with remote master
    
    PJ : So does this mean you can track all the remote branches in puppet although you have a few local branches. What's the many "tracked" signs mean you see in the result of the command? "tracked" by which local branch?
    Jeremy Bouse : The remote branches are tracked in that if you do a `git fetch` or `git pull` updates to the remote branches will be tracked in your cloned repository. The local branches are just that, local branches of the remote branches and thus updates to the remote branches will be tracked and merged in when the appropriate command to do so is given. I explicitly include the '-t' option when making the local branch to ensure it tracks the branch from which it originated from. Remember a local branch can also track another local branch so doesn't have to be a remote branch.
    Chris Johnsen : @PJ: The term “track” has two distinct meanings in Git. The “tracked” lines in `git remote show remote-name` refer to [“tracking branches”](http://www.kernel.org/pub/software/scm/git/docs/gitglossary.html#def_tracking_branch) (snapshots of branches from remote repositories). The “merges with” lines refer to local branches that have an [“upstream branch”](http://www.kernel.org/pub/software/scm/git/docs/gitglossary.html#def_upstream_branch) configuration (made with with the `--track`/`-t` option of *git branch* or *git checkout* and thus often confused with “tracking branches”).
  • Jeremy Bouse illustrates how git remote show displays tracking information. That should be sufficient if you only want the information for human consumption.

    If you plan on using the information in an automated context (e.g. a script) you should use the lower-level (“plumbing”) git for-each-ref instead.

    % git remote show origin
    * remote origin
    ⋮
      Local branches configured for 'git pull':
        master merges with remote master
        pu     merges with remote pu
    ⋮
    % git for-each-ref --format='%(refname:short) <- %(upstream:short)' refs/heads
    master <- origin/master
    pu <- origin/pu
    

    The git for-each-ref learned the %(upstream) token in Git 1.6.3. With earlier versions of Git you will have to extract the tracking information with git config branch.<name>.remote and git config branch.<name>.merge (probably using git for-each-ref to build the commands for each local branch name).

0 comments:

Post a Comment