3679

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.

Pat Notz
  • 186,044
  • 29
  • 86
  • 92
  • 27
    As noted below, for an existing branch, you can use `git push -u origin branch-name`. – Zags Mar 06 '14 at 23:46
  • 3
    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 – billrichards Aug 07 '15 at 12:59
  • 65
    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. – Felipe Alvarez Mar 18 '17 at 13:39
  • 9
    as of Git 2.10 you should first checkout to intended local branch and then do this `git branch --set-upstream-to origin/` – Mahdi Javaheri Sep 18 '17 at 04: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/` is the current command that works. – Super Jade Aug 24 '18 at 17:47
  • For people getting this SO question as first hit searching for `"git remote add" "tracking"`: I needed to add a new remote tracking an existing branch, which I solved with `git remote add gitlab https://user@gitlab.com/group/subgroup/repository.git -t master` followed by `git push gitlab`. Works with `git version 2.18.0`. The `-t` is for `tracking` as there is no `--tracking` according to `man git remote add` – Jeroen Wiert Pluimers Sep 17 '18 at 08:11
  • it is definitely frustrating that checking out out a branch or initiating gitflow intermittently does not link the branch to the remote source and will randomly throw errors instead of completing normal commands. This breaks setup scripts, or baffles newbies who are told "follow these commands to get started". – ppostma1 Jun 20 '19 at 21:41
  • Does this answer your question? [How do I push a new local branch to a remote Git repository and track it too?](https://stackoverflow.com/questions/2765421/how-do-i-push-a-new-local-branch-to-a-remote-git-repository-and-track-it-too) – Henke Dec 05 '20 at 08:04

22 Answers22

4432

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?

isapir
  • 14,475
  • 6
  • 82
  • 97
Dan Moulding
  • 183,296
  • 21
  • 89
  • 97
  • 138
    Is "upstream" the name of the remote? i.e. what most would call "origin" by default? – Andrew Vit Jun 26 '10 at 06:30
  • 179
    @Andrew: Yes. `git branch --set-upstream master origin/master` would be equivalent to what is automatically done when you initially clone a repository. – Dan Moulding Jun 26 '10 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 05 '10 at 04:31
  • 61
    I get "fatal: Not a valid object name: 'origin/master'." – joachim Mar 24 '11 at 22:07
  • 2
    Just in case someone didn't know: "git branch -a -v" shows what branches you have available, "git remote -v" shows what remotes you have. – Ondra Žižka May 09 '11 at 17:22
  • 5
    joachim: do you have a remote called "origin" with a branch "master" in it? `git branch -r` will show your remote branches. – Matt Connolly Jul 25 '11 at 05:53
  • 4
    joachim: I got the same error. In my case, I was changing servers for remote origin, so I renamed the remote. Then there was no more origin/master remote branch, so I needed to do this: git fetch origin git branch --set-upstream master origin/master git pull – Liam Aug 17 '11 at 15:11
  • 88
    `git push -u origin foo` [via](http://stackoverflow.com/questions/6089294/git-why-do-i-need-to-do-set-upstream-all-the-time/6089415#6089415) – Cotton Sep 21 '11 at 07:02
  • @PaulHedderly (just to confirm), this command literally does nothing more than what your answer does? – Alexander Bird Jan 08 '12 at 04:49
  • @joachim same error, I simply had to start by typing "git push origin master", because at first there was no master branch in the remote origin. – Unfalkster Jan 23 '12 at 16:50
  • For future reference: if you get "fatal: Ambigious object name: ", add `remotes/` before it. So instead of `git branch --set-upstream master origin/master`, do `git branch --set-upstream master remotes/origin/master`. – Erik Hesselink Jan 27 '12 at 09:29
  • @ErikHesselink I still get the ambiguous error, even with remotes/origin/master: warning: refname 'remotes/origin/master' is ambiguous. fatal: Ambiguous object name: 'remotes/origin/master'. – pupeno Feb 12 '12 at 14:16
  • @J.PabloFernández: Try adding 'refs/' in front of the remote branch name, making it 'refs/remotes/origin/master'. – Erik Hesselink Feb 13 '12 at 09:31
  • @ErikHesselink that din't help: git branch --set-upstream master refs/remotes/origin/master error: Not tracking: ambiguous information for ref refs/remotes/origin/master – pupeno Feb 13 '12 at 18:51
  • @ErikHesselink nevermind... somehow I ended up with a messed up repo. – pupeno Feb 13 '12 at 18:54
  • 3
    Also in recent `git`, if you have fetched new remote branch and don't have a local branch for it, then `git checkout` with the same branchname will auto created a tracking branch – Eugene Jan 15 '13 at 12:16
  • To display your origin and upstream, type `git remote -v` – KrisWebDev Jul 20 '13 at 09:53
  • @joachim See: [Why does git not recognize “origin/master” as a valid object name?](http://stackoverflow.com/q/14717957/1851186) – TachyonVortex Dec 18 '13 at 13:23
  • 8
    I use Git 1.8.1.2 and it gives me: `The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to` – guneysus Dec 20 '13 at 12:50
  • 1
    @ChrisFrancis Git never ceases to amaze me with its powerful and flexible features, accessed with a cryptic but surprisingly simple syntax. Sometimes when I come here I feel like I'm at Hogwarts learning new spells... – bearvarine Feb 26 '14 at 19:18
  • This actually works with git-svn as well, though you may need to do a little bit of work (like a regular git rebase) to get your local branch to no longer be diverged from the new svn branch. – NeoSkye Mar 01 '14 at 00:50
  • @AndrewVit I always set my remote repo (e.g. GitHub) to `origin` and the repo I forked from as `upstream`. Then I `git fetch upstream` and `git branch -u origin/foo` – flying sheep Dec 30 '14 at 17:05
  • 2
    This doesn't work correctly in git 2.8.1. It tries to create a local branch called upstream/foo, even when upstream is a remote repo. – cmcginty Apr 29 '16 at 01:42
  • The 1st and 3rd solutions will only work when `foo` is the current branch [according to the docs](https://git-scm.com/docs/git-branch#git-branch--ultupstreamgt), even [in 1.8](https://git-scm.com/docs/git-branch/1.8.0#git-branch--ultupstreamgt). Since the question does not specify whether the "existing branch" is checked out, I feel that `git branch -u upstream/foo` and `git branch --set-upstream-to=upstream/foo` should be **removed** from this answer. – John E Feb 18 '19 at 20:52
  • `git branch -u upstream/foo` works for me , not try others. – hukeping Oct 06 '19 at 10:42
  • Also it can be used as: `git branch -u origin/branch_name` – Taras Melnyk Jun 11 '20 at 12:50
  • Why don't git push to this branch by default? I have a `sth` branch, tracking a remote's `master`. `git push` and `git push personal master` don't work, but a lengthy `git push rem HEAD:master` works. – Minh Nghĩa Jun 17 '20 at 18:43
247

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
Josh
  • 1,752
  • 1
  • 18
  • 20
Paul Hedderly
  • 3,437
  • 2
  • 15
  • 12
  • 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 ? – Doppelganger Mar 02 '10 at 05:14
  • 5
    Yes - but from memory you may need to be explicit for the first push. Can easily be tested of course... :) – Paul Hedderly Jun 18 '10 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 '12 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 '14 at 23:02
  • 4
    @SteveK that's most likely because your upstream is called `origin` and not `upstream`. – umläute Aug 11 '15 at 14:21
171

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
Saurav Sahu
  • 9,755
  • 5
  • 42
  • 64
Greg Bacon
  • 121,231
  • 29
  • 179
  • 236
62

For Git versions 1.8.0 and higher:

Actually for the accepted answer to work:

git remote add upstream <remote-url>
git fetch upstream
git branch -f --track qa upstream/qa
# OR Git version 1.8.0 and higher:
git branch --set-upstream-to=upstream/qa
# Gitversions lower than 1.8.0
git branch --set-upstream qa upstream/qa
Hedgehog
  • 4,900
  • 3
  • 30
  • 38
  • The local branch was already tracking a branch, so we can assume the remote repo was already added. – Doppelganger May 18 '11 at 21:15
  • 1
    Dopplerganger: See joachim's comment to the accepted answer. Anyway assumptions readily differ - it what makes things so interesting ;) – Hedgehog Jun 13 '11 at 23:20
58

You might find the git_remote_branch tool useful. It offers simple commands for creating, publishing, deleting, tracking & renaming remote branches. One nice feature is that you can ask a grb command to explain what git commands it would execute.

grb explain create my_branch github
# git_remote_branch version 0.3.0

# List of operations to do to create a new remote branch and track it locally:
git push github master:refs/heads/my_branch
git fetch github
git branch --track my_branch github/my_branch
git checkout my_branch
James Mead
  • 3,384
  • 17
  • 17
48

I believe that in as early as Git 1.5.x you could make a local branch $BRANCH track a remote branch origin/$BRANCH, like this.

Given that $BRANCH and origin/$BRANCH exist, and you've not currently checked out $BRANCH (switch away if you have), do:

git branch -f --track $BRANCH origin/$BRANCH

This recreates $BRANCH as a tracking branch. The -f forces the creation despite $BRANCH existing already. --track is optional if the usual defaults are in place (that is, the git-config parameter branch.autosetupmerge is true).

Note, if origin/$BRANCH doesn't exist yet, you can create it by pushing your local $BRANCH into the remote repository with:

git push origin $BRANCH

Followed by the previous command to promote the local branch into a tracking branch.

wu-lee
  • 581
  • 4
  • 2
  • 1
    `git push origin $BRANCH` was what I was looking for. – User Apr 02 '13 at 15:15
  • After trying all sorts of solutions, including setting up an upstream as described above nothing worked. All I wanted to do is pull 1 new commit into my local branch from a remote one and I didn't setup tracking initially. The command `git branch -f --track $BRANCH origin/$BRANCH` does the trick. – DemitryT Feb 13 '14 at 15:08
45

1- update your local meta-data using : git fetch --all

enter image description here

2- show your remote and local branches using : git branch -a , see the following Screenshot

enter image description here

3- switch to target branch , that you want to linked with the remote: using

git checkout branchName

example :

enter image description here

4- Link your local branch to a remote branch using:

git branch --set-upstream-to nameOfRemoteBranch

N.B : nameOfRemoteBranch : to copy from the output of step 2 " git branch -r "

Example of use:

enter image description here

Isaac
  • 10,133
  • 5
  • 27
  • 43
Monsif EL AISSOUSSI
  • 1,861
  • 13
  • 14
27

Make sure you run :

git config push.default tracking

to be able to push trouble free

romanlv
  • 1,282
  • 13
  • 15
  • 1
    This could be convenient. We could note, however, that according to `git-config(1)` manual page, `tracking` is deprecated synonym of `upstream`. – FooF Aug 08 '12 at 02:39
25

Editing .git/config is probably the easiest and fastest way. That's what the Git commands for handling remote branches are doing, anyway.

If you don't want to muck with the file by hand (and it's not that hard to do), you can always use git config to do it...but again, that's just going to edit the .git/config file, anyway.

There are, of course, ways to automatically track a remote branch when using git checkout (by passing the --track flag, for example), but these commands work with new branches, not existing ones.

mipadi
  • 359,228
  • 81
  • 502
  • 469
20

In very short

git branch --set-upstream yourLocalBranchName origin/develop

This will make your yourLocalBranchName track the remote branch called develop.

Acumenus
  • 41,481
  • 14
  • 116
  • 107
MadNik
  • 7,273
  • 2
  • 33
  • 37
  • 1
    @Quincy Check [greg's answer](http://stackoverflow.com/a/12694311/321973) - use `git push -u origin branch` (or `--set-upstream-to`) instead – Tobias Kienzler May 28 '13 at 08:37
  • @MadNik, what is the difference between `--set-upstream` and `--track`? I don't quite understand why I should use one over the other. – Acumenus Mar 31 '14 at 18:30
17

For 1.6.x, it can be done using the git_remote_branch tool:

grb track foo upstream

That will cause Git to make foo track upstream/foo.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
wik
  • 2,372
  • 1
  • 23
  • 26
14

I use the following command (Suppose your local branch name is "branch-name-local" and remote branch name is "branch-name-remote"):

$ git branch --set-upstream-to=origin/branch-name-remote branch-name-local

If both local and remote branches have the same name, then just do the following:

$ git branch --set-upstream-to=origin/branch-name branch-name
yrazlik
  • 9,023
  • 26
  • 84
  • 145
  • You reversed "branch-name" and "origin/branch-name" on the command line. The upstream comes before the local. – maharvey67 Oct 10 '18 at 22:36
  • @maharvey67 you are right, thanks. Edited the answer. – yrazlik Oct 12 '18 at 10:03
  • This was gold, thank you, also because none of the options within the accepted answer are ok when you have slashes in the branch name – JBoy Jul 15 '19 at 14:53
10

For creating new branch, we could use following command

 git checkout --track -b example origin/example 
For the already created branch to create link between remote then from that branch use below command

 git branch -u origin/remote-branch-name
jithu reddy
  • 361
  • 3
  • 16
9

Here, using github and git version 2.1.4, just do:

$ git clone git@github.com:user/repo.git

And remotes come by itelsef, even if not linked locally:

$ git remote show origin

* remote origin
  Fetch URL: git@github.com:user/repo.git
  Push  URL: git@github.com:user/repo.git
  HEAD branch: master
  Remote branches:
    develop tracked         <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    master  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)

But of course, still no local branch:

$ git branch
* master                  <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

See? Now if you just checkout develp, it will do the magic automatically:

$ git checkout develop
Branch develop set up to track remote branch develop from origin.
Switched to a new branch 'develop'

So easy!


Summary. Just run this 2 commands:

$ git clone git@github.com:user/repo.git
$ git checkout develop
DrBeco
  • 9,819
  • 7
  • 52
  • 70
  • 1
    An excellent example of my identical use case. Despite no sign of a local 'develop' branch, when I checked out the 'develop' branch that branch appears and is magically set up to track the remote branch 'develop' from origin. I appreciate the step by step example and explanation! – ElliotPsyIT Mar 27 '18 at 20:48
9

Use '--track' Option

  • After a git pull :

    git checkout --track <remote-branch-name>

  • Or:

    git fetch && git checkout <branch-name>

Loukan ElKadi
  • 2,017
  • 14
  • 16
6

This isn't a direct answer to this question, but I wanted to leave a note here for anyone who may be having the same issue as me when trying to configure an upstream branch.

Be wary of push.default.

With older git versions, the default was matching, which would cause very undesirable behaviour if you have, for example:

Local branch "master" tracking to origin/master

Remote branch "upstream" tracking to upstream/master

If you tried to "git push" when on the "upstream" branch, with push.default matching git would automatically try to merge the local branch "master" into "upstream/master", causing a whole lot of chaos.

This gives more sane behaviour:

git config --global push.default upstream

Tom Mettam
  • 2,454
  • 1
  • 23
  • 30
5

In a somewhat related way I was trying to add a remote tracking branch to an existing branch, but did not have access to that remote repository on the system where I wanted to add that remote tracking branch on (because I frequently export a copy of this repo via sneakernet to another system that has the access to push to that remote). I found that there was no way to force adding a remote branch on the local that hadn't been fetched yet (so local did not know that the branch existed on the remote and I would get the error: the requested upstream branch 'origin/remotebranchname' does not exist).

In the end I managed to add the new, previously unknown remote branch (without fetching) by adding a new head file at .git/refs/remotes/origin/remotebranchname and then copying the ref (eyeballing was quickest, lame as it was ;-) from the system with access to the origin repo to the workstation (with the local repo where I was adding the remote branch on).

Once that was done, I could then use git branch --set-upstream-to=origin/remotebranchname

Ville
  • 3,657
  • 1
  • 32
  • 36
5

For git version 2.25.1, use the command:

git push --set-upstream origin <local_branch_name>
iknow
  • 4,796
  • 9
  • 19
  • 37
tonderaimuchada
  • 117
  • 1
  • 6
4

or simply by :

switch to the branch if you are not in it already:

[za]$ git checkout branch_name

run

[za]$ git branch --set-upstream origin branch_name
Branch origin set up to track local branch brnach_name by rebasing.

and you ready to :

 [za]$ git push origin branch_name

You can alawys take a look at the config file to see what is tracking what by running:

 [za]$ git config -e

It's also nice to know this, it shows which branches are tracked and which ones are not. :

  [za]$ git remote show origin 
z atef
  • 5,570
  • 3
  • 41
  • 40
1

For anyone who, like me, just wants to sync up your local branch name with the remote branch name, here's a handy command:

git branch -u origin/$(git rev-parse --abbrev-ref HEAD)
Constantinos
  • 1,007
  • 9
  • 15
0

To avoid remembering what you need to do each time you get the message:

Please specify which branch you want to merge with. See git-pull(1)
for details.
.....

You can use the following script which sets origin as upstream for the current branch you are in.

In my case I almost never set something else than origin as the default upstream. Also I almost always keep the same branch name for local and remote branch. So the following fits me:

#!/bin/bash
# scriptname: git-branch-set-originupstream
current_branch="$(git branch | grep -oP '(?<=^\* )(.*)$')"
upstream="origin/$current_branch"
git branch -u "$upstream"
Marinos An
  • 6,191
  • 2
  • 35
  • 68
-2

This would work too

git branch --set-upstream-to=/< remote>/< branch> < localbranch>
Mad Physicist
  • 76,709
  • 19
  • 122
  • 186
Abhi
  • 197
  • 2
  • 8