Setting a Default Upstream Branch in Git

So you’re working in a branch in your Git repository, and you’ve just made some changes. You just ran git commit and now you want to merge any changes from upstream. So you issue a git pull and BAM!, you see this error:

# git pull
You asked me to pull without telling me which branch you
want to merge with, and 'branch.xyzbranch.merge' in
your configuration file does not tell me, either. Please
specify which branch you want to use on the command line and
try again (e.g. 'git pull  ').
See git-pull(1) for details.

If you often merge with the same branch, you may want to
use something like the following in your configuration file:
    [branch "xyzbranch"]
    remote = 
    merge = 

    [remote ""]
    url = 
    fetch = 

See git-config(1) for details.

So what gives? Why doesn’t git know what you’re trying to do?

Where is My Upstream Branch?

The problem is git has no idea which branch you want to pull from or push to. Chances are you made this branch like this:

# git branch xyzbranch

That creates a new branch in your git repository, but it’s not set up to track any remote (upstream) branches. You can use git remote show origin to see where your branches are pushing and pulling if you’d like to verify that.

# git remote show origin
* remote origin
  Fetch URL: git@hostname:repository.git
  Push  URL: git@hostname:repository.git
  HEAD branch: master
  Remote branches:
    another_branch    tracked
    master            tracked
    some_other_branch tracked
  Local branches configured for 'git pull':
    another_branch    merges with remote another_branch
    some_other_branch merges with remote some_other_branch
  Local refs configured for 'git push':
    another_branch    pushes to another_branch (fast-forwardable)
    master            pushes to master (up to date)
    some_other_branch pushes to some_other_branch (up to date)

So there is no upstream branch set for xyzbranch! That’s not really a problem, it just means you have to tell git where to pull changes from and where to push them to every time you use git pull and git push. But that’s annoying, so how do we fix it and set a default remote branch?

Setting Your Upstream Branch

If you’re on a branch called xyzbranch and you want to track the master branch, you could use git branch –set-upstream to do that like this:

# git fetch origin
# git branch --set-upstream xyzbranch origin/master
Branch xyzbranch set up to track remote branch master from origin.

And suddenly, voila!

# git pull
Already up-to-date.

No more complaining from git!

For more tips and tricks on Git, check out GitReady.com.

Git it done!

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright Tech 2017