Search code examples
gitsettingsgit-remote

How to automatically track a remote branch when creating a local branch with the same name?


I am using Git on several machines. I must have a configuration, which defers on one of the machines, because in the scenario, where I am on master and have a remote branch origin/foo and want to create a local branch foo, which tracks the remote branch the following happens:

  • on most of the machines the command I need to use is (while on master):

    git checkout -b foo origin/foo
    
  • however on one of the machines it suffices if I just use:

    git checkout -b foo
    

In both cases I get

Branch foo set up to track remote branch foo from origin.

However, the second command works only on one of my machines and on the rest of them it just checks out a local branch foo. Why is that and which setting is responsible for it to work?

P.S. I did the following things, which didn't help:

  • updated to git v. 2.1.3;
  • made sure I have only one remote;
  • fetched the remote branches with git fetch origin

UPDATE The information that on one of the machines git checkout -b foo works is false. The correct command to checkout a local branch to track a remote is git checkout foo without -b.


Solution

  • Check if the git version is an older one.

    Recent versions of Git should always create a tracking branch with git checkout -b:

    If <branch> is not found but there does exist a tracking branch in exactly one remote (call it <remote>) with a matching name, treat as equivalent to

    git checkout -b <branch> --track <remote>/<branch>
    

    (or foo is present in more than one remote)
    (or you didn't fetch from that remote yet, and it didn't detect the presence of the remote tracking branch origin/foo)

    However, it uses git checkout foo, not git checkout -b foo, as the OP Alex Popov confirms in the comments.