Categories
branch git git-branch

Make an existing Git branch track a remote branch?

3858

I know how to make a new branch that tracks remote branches, but how do I make an existing branch track a remote branch?

I know I can just edit the .git/config file, but it seems there should be an easier way.

9

  • 28

    As noted below, for an existing branch, you can use git push -u origin branch-name.

    – Zags

    Mar 6, 2014 at 23:46

  • 4

    If local branch is current branch, and local branch is not already tracking a remote, git pull will often provide helpful messages about the appropriate command to set tracking information

    Aug 7, 2015 at 12:59


  • 82

    It’s annoying when one is learning git to be shown a link to the git documentation. That documentation appears to be written for people that already know what they are doing with git.

    Mar 18, 2017 at 13:39

  • 10

    as of Git 2.10 you should first checkout to intended local branch and then do this git branch --set-upstream-to origin/<branch>

    Sep 18, 2017 at 4:59

  • 3

    --set-upstream produces an error: fatal: the '--set-upstream' option is no longer supported. Please use '--track' or '--set-upstream-to' instead. so git branch --set-upstream-to origin/<branch name> is the current command that works.

    Aug 24, 2018 at 17:47

4645

Given a branch foo and a remote upstream:

As of Git 1.8.0:

git branch -u upstream/foo

Or, if local branch foo is not the current branch:

git branch -u upstream/foo foo

Or, if you like to type longer commands, these are equivalent to the above two:

git branch --set-upstream-to=upstream/foo

git branch --set-upstream-to=upstream/foo foo

As of Git 1.7.0 (before 1.8.0):

git branch --set-upstream foo upstream/foo

Notes:

  • All of the above commands will cause local branch foo to track remote branch foo from remote upstream.
  • The old (1.7.x) syntax is deprecated in favor of the new (1.8+) syntax. The new syntax is intended to be more intuitive and easier to remember.
  • Defining an upstream branch will fail when run against newly-created remotes that have not already been fetched. In that case, run git fetch upstream beforehand.

See also: Why do I need to do `–set-upstream` all the time?

28

  • 146

    Is “upstream” the name of the remote? i.e. what most would call “origin” by default?

    Jun 26, 2010 at 6:30

  • 183

    @Andrew: Yes. git branch --set-upstream master origin/master would be equivalent to what is automatically done when you initially clone a repository.

    Jun 26, 2010 at 10:09

  • 63

    On a related note, adding this to your gitconfig is awesome: “[push] default=tracking” this will make it so that pushes will go the same place that pulls come from 🙂

    – jpswain

    Dec 5, 2010 at 4:31


  • 61

    I get “fatal: Not a valid object name: ‘origin/master’.”

    – joachim

    Mar 24, 2011 at 22:07

  • 93

    git push -u origin foo via

    – Cotton

    Sep 21, 2011 at 7:02


261

You can do the following (assuming you are checked out on master and want to push to a remote branch master):

Set up the ‘remote’ if you don’t have it already

git remote add origin ssh://...

Now configure master to know to track:

git config branch.master.remote origin
git config branch.master.merge refs/heads/master

And push:

git push origin master

6

  • it is really required the remote and the branch in the push? I mean, you only need it if your checked out branch is not the one you want to push, right ?

    Mar 2, 2010 at 5:14

  • 5

    Yes – but from memory you may need to be explicit for the first push. Can easily be tested of course… 🙂

    Jun 18, 2010 at 20:57

  • +1 This is the answer for Windows users who are stuck with the msysgit “preview” that is pre 1.8. Thanks for that.

    – John

    Oct 16, 2012 at 12:33

  • 3

    This is the only answer that worked for me. When I tried the accepted answer, to set the upstream remote for an existing branch, I got: error: the requested upstream branch 'upstream/master' does not exist.

    – Steve K

    May 13, 2014 at 23:02


  • 4

    @SteveK that’s most likely because your upstream is called origin and not upstream.

    – umläute

    Aug 11, 2015 at 14:21

179

I do this as a side-effect of pushing with the -u option as in

$ git push -u origin branch-name

The equivalent long option is --set-upstream.

The git-branch command also understands --set-upstream, but its use can be confusing. Version 1.8.0 modifies the interface.

git branch --set-upstream is deprecated and may be removed in a relatively distant future. git branch [-u|--set-upstream-to] has been introduced with a saner order of arguments.

It was tempting to say git branch --set-upstream origin/master, but that tells Git to arrange the local branch “origin/master” to integrate with the currently checked out branch, which is highly unlikely what the user meant. The option is deprecated; use the new --set-upstream-to (with a short-and-sweet -u) option instead.

Say you have a local foo branch and want it to treat the branch by the same name as its upstream. Make this happen with

$ git branch foo
$ git branch --set-upstream-to=origin/foo

or just

$ git branch --set-upstream-to=origin/foo foo

0