2048

I create a new branch in Git:

git branch my_branch

Push it:

git push origin my_branch

Now say someone made some changes on the server and I want to pull from origin/my_branch. I do:

git pull

But I get:

You asked me to pull without telling me which branch you
want to merge with, and 'branch.my_branch.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 <repository> <refspec>').
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 "my_branch"]
    remote = <nickname>
    merge = <remote-ref>

    [remote "<nickname>"]
    url = <url>
    fetch = <refspec>

See git-config(1) for details.

I learned that I can make it work with:

git branch --set-upstream my_branch origin/my_branch

But why do I need to do this for every branch I create? Isn't it obvious that if I push my_branch into origin/my_branch, then I would want to pull origin/my_branch into my_branch? How can I make this the default behavior?

12
  • 34
    The default for branch.autosetupmerge means that the upstream configuration for a new branch is only automatically set when creating a branch from a remote-tracking branch (e.g. <remote-name>/<branch-name>) (see git-config(1)). You are probably creating your branches from existing local branches. If you are effectively branching directly from the tip of a remote branch (despite being on a local branch), then you can use git branch my_branch <remote-name>/<branch-name> to automatically setup the upstream configuration. Commented May 23, 2011 at 4:16
  • 27
    FYI, the --set-upstream option is deprecated. You should use --track or --set-upstream-to instead. Commented Aug 12, 2014 at 15:00
  • 222
    if --set-upstream is deprecated, then perhaps the git devs should remove it from the help message that gets displayed when you run git push with no options and no upstream is set? Commented Sep 15, 2016 at 0:15
  • 24
    @ChristopherHunter It's been over a year since your comment and it still says that. Is it just a sloppy feedback or perhaps is there a technically wise reason to keep it around that we're ignorant about? Commented Nov 13, 2016 at 15:19
  • 51
    @ChristopherHunter git branch --set-upstream is deprecated. git push --set-upstream is not.
    – Rag
    Commented Sep 19, 2018 at 0:24

25 Answers 25

2075

Git v2.37.1 and above

If you are using the mentioned version or above you can use this new config entry to automatically setup remote tracking:

git config --global push.autoSetupRemote true

After that, when you do git push tracking is setup automatically. No need for git push -u origin my_branch


A shortcut, which doesn't depend on remembering the syntax for git branch --set-upstream 1 is to do:

git push -u origin my_branch

... the first time that you push that branch. Or, to push to the current branch from a branch of the same name (handy for an alias):

git push -u origin HEAD

You only need to use -u once, and that sets up the association between your branch and the one at origin in the same way as git branch --set-upstream does.

Personally, I think it's a good thing to have to set up that association between your branch and one on the remote explicitly. It's just a shame that the rules are different for git push and git pull.


1 It may sound silly, but I very frequently forget to specify the current branch, assuming that's the default - it's not, and the results are most confusing.

Update 2012-10-11: Apparently I'm not the only person who found it easy to get wrong! Thanks to VonC for pointing out that git 1.8.0 introduces the more obvious git branch --set-upstream-to, which can be used as follows, if you're on the branch my_branch:

git branch --set-upstream-to origin/my_branch

... or with the short option:

git branch -u origin/my_branch

This change, and its reasoning, is described in the release notes for git 1.8.0, release candidate 1:

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 to be what the user meant. The option is deprecated; use the new --set-upstream-to (with a short-and-sweet -u) option instead.

23
  • 114
    Also note that even if you forget the -u the first time you push, you can run the push again with that flag and it will start tracking.
    – Henrik N
    Commented Jan 12, 2013 at 17:33
  • 89
    None of these satisfy the use-case of using git push without arguments. It remains that I still have to remember to 'git push -u origin my-branch' when moving my new branch to the remote for the first time. Commented May 9, 2013 at 16:15
  • 9
    Note to self: a more complete explanation for the first git push -u origin master: www.greatytc.com/a/17096880/6309
    – VonC
    Commented Jun 14, 2013 at 19:56
  • 30
    I hate remembering that syntax as well, so I created the following alias: alias gpo="git push --set-upstream origin $(git branch | awk '/^\* / { print $2 }')" Commented Oct 16, 2013 at 18:44
  • 150
    This is all fine, but I still think the OP's complaint is valid. You start a local branch, work on it, push it to origin to share (with no args); why shouldn't that set the upstream? Is it actually desirable for some reason NOT to set upstream when pushing a new branch to a remote?
    – GaryO
    Commented Nov 15, 2013 at 16:30
1819

You can make this happen with less typing. First, change the way your push works:

git config --global push.default current

This will infer the origin my_branch part, thus you can do:

git push -u

Which will both create the remote branch with the same name and track it.

17
  • 7
    How come can git infer origin when running git push -u for newly created branch in newly created repository? Is the assumption that repository had been cloned thus current branch has its remote set to origin? Commented Oct 11, 2015 at 11:03
  • 129
    This should be the default. So many things in git could be more user-friendly if it just shipped with better defaults.
    – phreakhead
    Commented Feb 11, 2016 at 22:29
  • 20
    Be aware that 'current' is slightly unsafer than using 'simple' to do the same thing, see www.greatytc.com/questions/23918062/…
    – Air
    Commented Mar 24, 2016 at 0:28
  • 37
    It does, but then when you try to pull you'll have to specify from where. The -u sets up the branch tracking between origin and your local repo.
    – Zamith
    Commented May 29, 2017 at 15:02
  • 41
    While marginally convenient, this still mandates that a different command be run for the first and only push– which defeats the entire point of this question. In short, there is no good answer. That Git developers insist on retaining this Awkward User eXperience (AUX) in the face of widespread community dissent is... enlightening. And discouraging. (Mostly discouraging.) Commented Dec 14, 2018 at 5:55
221

This is my most common use for The Fuck.

$ git push
fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin master

$ fuck
git push --set-upstream origin master [enter/↑/↓/ctrl+c]
Counting objects: 9, done.
...

Also, it's fun to type swear words in your terminal.

2
  • 7
    Fun fact: only 2.4% of programmers disagree with the last statement
    – mirekphd
    Commented Aug 6, 2022 at 8:28
  • 2
    If you are pair programming you might want to set a different alias :-D To do that you can set the alias in your .bashrc or .zshrc config like this eval $(thefuck --alias fix) I have set the alias as "fix" instead of the default one "fuck" Ref - github.com/nvbn/thefuck#manual-installation
    – Kishan B
    Commented Feb 9, 2023 at 11:56
129
git config --global push.autoSetupRemote true

The OP asks:

I learned that I can make it work with:

git branch --set-upstream my_branch origin/my_branch

But why do I need to do this for every branch I create?

You do not need to set upstream all the time.
Not anymore (eleven years later).

With Git 2.37 (Q3 2022), a git config --global push.autoSetupRemote true will take care of that for you.

See commit 05d5775, commit 8a649be, commit bdaf1df (29 Apr 2022) by Tao Klerks (TaoK).
(Merged by Junio C Hamano -- gitster -- in commit f49c478, 26 May 2022)

push: new config option "push.autoSetupRemote" supports "simple" push

Signed-off-by: Tao Klerks

In some "simple" centralized workflows, users expect remote tracking branch names to match local branch names.
"git push"(man) pushes to the remote version/instance of the branch, and "git pull"(man) pulls any changes to the remote branch (changes made by the same user in another place, or by other users).

This expectation is supported by the push.default default option "simple" which refuses a default push for a mismatching tracking branch name, and by the new branch.autosetupmerge option, "simple", which only sets up remote tracking for same-name remote branches.

When a new branch has been created by the user and has not yet been pushed (and push.default is not set to "current"), the user is prompted with a "The current branch %s has no upstream branch" error, and instructions on how to push and add tracking.

This error is helpful in that following the advice once per branch "resolves" the issue for that branch forever, but inconvenient in that for the "simple" centralized workflow, this is always the right thing to do, so it would be better to just do it.

Support this workflow with a new config setting, push.autoSetupRemote, which will cause a default push, when there is no remote tracking branch configured, to push to the same-name on the remote and --set-upstream.

Also add a hint offering this new option when the "The current branch %s has no upstream branch" error is encountered, and add corresponding tests.

The new hint is:

To have this happen automatically for branches without a tracking
upstream, see 'push.autoSetupRemote' in 'git help config'

git config now includes in its man page:

push.autoSetupRemote

If set to "true" assume --set-upstream on default push when no upstream tracking exists for the current branch;

This option takes effect with push.default options 'simple', 'upstream', and 'current'.

It is useful if by default you want new branches to be pushed to the default remote (like the behavior of 'push.default=current') and you also want the upstream tracking to be set.
Workflows most likely to benefit from this option are 'simple' central workflows where all branches are expected to have the same name on the remote.

1
  • 8
    This answer should be the accepted one: doing git config --global push.autoSetupRemote always will let you auto track origin as soon as you push a branch/commit.
    – bneil
    Commented Aug 11, 2022 at 23:46
105

You can simply

git checkout -b my-branch origin/whatever

in the first place. If you set branch.autosetupmerge or branch.autosetuprebase (my favorite) to always (default is true), my-branch will automatically track origin/whatever.

See git help config.

7
  • 6
    This produces "fatal: Cannot update paths and switch to branch 'my-branch' at the same time." Commented May 9, 2013 at 15:55
  • 14
    By the way, I usually just git checkout -t origin/whatever, which also chooses whatever as the new branch-name. Very convenient!
    – cdunn2001
    Commented May 10, 2013 at 4:35
  • 2
    @cdunn This one is ideal, but hardly consistent. The flag should be called -u/--set-upstream.
    – Tobu
    Commented Jun 6, 2013 at 22:38
  • 2
    git checkout -t origin/whatever doesn't work for me when trying to create a new branch: fatal: Cannot update paths and switch to branch 'whatever' at the same time.
    – wisbucky
    Commented May 10, 2018 at 22:53
  • 2
    branch.autosetupmerge = false did not work either. It set the upstream tracking to local whatever branch instead of remote origin/whatever.
    – wisbucky
    Commented May 10, 2018 at 23:27
94

You can set upstream simpler in two ways. First when you create the branch:

git branch -u origin/my-branch

or after you have created a branch, you can use this command.

git push -u origin my-branch

You can also branch, check out and set upstream in a single command:

git checkout -b my-branch -t origin/my-branch

My personally preference is to do this in a two-step command:

git checkout -b my-branch
git push -u origin my-branch
6
  • 1
    Great answer! Addresses both common use cases. After running git branch -u origin/my-branch I can run git pull to pull down my changes. Commented Dec 2, 2016 at 22:22
  • 6
    "git checkout -b my-branch -t origin/my-branch" this doesn't work if 'origin/my-branch' doesn't exist yet.
    – Spongman
    Commented Nov 16, 2017 at 18:43
  • 1
    You actually can just do git checkout -t origin/my-branch without the -b my-branch, it will just automatically infer my-branch for the local branch name. However, as @Spongman mentioned, this command doesn't work if origin/my-branch doesn't exist first.
    – wisbucky
    Commented May 10, 2018 at 23:11
  • 1
    Yes, will work @wisbucky, -t works just fine. Personally though, even two years after I wrote that reply though, I still prefer splitting in two lines with checkout -b and push -u. It is more explicit and no error on checkout -b when I don't have remote - which happens quite often when experimenting :)
    – Tzen
    Commented May 15, 2018 at 7:32
  • 2
    git push -u origin/my-branch fails for me with fatal: 'origin/my-branch' does not appear to be a git repository. This works: git push -u origin my-branch
    – stason
    Commented Oct 26, 2018 at 20:29
66

You can use:

git config --global branch.autosetupmerge always

which will link the upstream branch each time you create or checkout a new branch.

See https://felipec.wordpress.com/2013/09/01/advanced-git-concepts-the-upstream-tracking-branch/

This also works with branch.autosetuprebase, if you follow a more rebase focused workflow, but don't use this unless you know what you're doing, as it will default your pull behavior to rebase, which can cause odd results.

5
  • 18
    Doesn't work, I still get the --set-upstream message
    – Dorian
    Commented Jul 29, 2015 at 15:16
  • 4
    @Dorian, You have to set this before you create the branch. See www.greatytc.com/a/9753268/263998
    – cdunn2001
    Commented Jun 22, 2016 at 15:52
  • I get: fatal: The upstream branch of your current branch does not match the name of your current branch...
    – Riscie
    Commented Sep 1, 2016 at 10:48
  • 9
    but this does not set the tracking branch as the remote with the same branch, but to the current local branch.. so when you do push it will try to push to the LOCAL branch you was before creating the new branch..
    – Arnold Roa
    Commented Aug 20, 2017 at 22:10
  • 23
    Be Careful with this setting!! After setting it, you get this behaviour. 1. Switch to master. 2. Run git checkout -b new_branch. 3. Add a commit to that branch. 4. git push origin new_branch. This pushes that commit to the master branch on origin (rather than to a new branch on origin called new_branch).
    – stwr667
    Commented Dec 6, 2019 at 3:30
44

By the way, the shortcut to pushing the current branch to a remote with the same name:

$ git push -u origin HEAD
0
36

I personally use these following alias in bash

in ~/.gitconfig file

[alias]
    pushup = "!git push --set-upstream origin $(git symbolic-ref --short HEAD)"

and in ~/.bashrc or ~/.zshrc file

alias gpo="git pushup"
alias gpof="gpo -f"
alias gf="git fetch"
alias gp="git pull"
2
  • 2
    I only needed to hcange .gitconfig, then I could use the command git pushup which always pushes the current branch to the origin. I can always just use git pushup instead of git push 👍 Commented Oct 12, 2018 at 20:52
  • 1
    just expanding on this answer to setup a global git alias (custom commands) - git config --global alias.pushup "!git push --set-upstream origin $(git symbolic-ref --short HEAD)"
    – Naren
    Commented Nov 10, 2020 at 20:50
35

If the below doesn't work:

git config --global push.default current

You should also update your project's local config, as its possible your project has local git configurations:

git config --local push.default current
7
  • 2
    More explanations would be great. What does the first line do?
    – papillon
    Commented Sep 3, 2019 at 7:40
  • 6
    This answer is the one that feels legit. All the ones proposing aliases are dumb workarounds. And the other ones justifying memorizing long command sequences are pedantic.
    – MarkHu
    Commented Nov 22, 2019 at 9:23
  • 2
    This works for pushing but not pulling. It uses the correct upstream when you push, but doesn't set the upstream, so you still have to run a command to set the upstream before you can pull a branch for the first time. Commented Feb 4, 2022 at 19:50
  • @rspeer not sure what you mean? This works for pull, but if you haven't done a fetch (or pull that also does fetch), you can not checkout a branch that was created remotely since last time you did a fetch - has and will always be the case. You could argue it's worth having a habit of running git fetch --prune, so you also get remote branches deleted locally if they are deleted remotely. Commented Mar 14, 2022 at 10:02
  • @JohnySkovdal What I mean is: if you run this configuration, create a branch, and push that branch, you cannot pull updates to that same branch that you created, because you didn't set its upstream. Lots of workflows could benefit from a way of automatically setting the upstream, which is what the question asked for. Commented Mar 22, 2022 at 16:02
13

For what it is worth, if you are trying to track a branch that already exists on the remote (eg. origin/somebranch) but haven't checked it out locally yet, you can do:

$ git checkout --track origin/somebranch

Note: '-t' is the shortened version of '--track' option.

This sets up the same association right off the bat.

3
  • 6
    You can actually just checkout to the branch. So git checkout somebranch is equivalent.
    – Zamith
    Commented Apr 8, 2014 at 10:15
  • 2
    @Zamith Doesn't that only work after having called git fetch immediately beforehand? Commented Mar 10, 2015 at 20:37
  • 2
    Not immediately, but yes, you do need to have a reference to that branch on your local repo, which happens whenever you call git fetch or git pull. I've never found that to be an issue, though.
    – Zamith
    Commented Mar 12, 2015 at 13:14
13

Update: push.autoSetupRemote now solves this in an easier way, finally! See the other answer on that here for more information.

Original answer:

I use this Git alias instead of copy/pasting the suggestion from Git every time: https://gist.github.com/ekilah/88a880c84a50b73bd306

Source copied below (add this to your ~/.gitconfig file):

[alias]
  pushup = "!gitbranchname() { git symbolic-ref --short HEAD; }; gitpushupstream() { git push --set-upstream origin `gitbranchname`; }; gitpushupstream"
10

You can also explicitly tell git pull what remote branch to pull (as it mentions in the error message):

git pull <remote-name> <remote-branch>

Be careful with this, however: if you are on a different branch and do an explicit pull, the refspec you pull will be merged into the branch you're on!

10
git branch --set-upstream-to=origin/master<branch_name>
1
  • Not supported anymore.
    – sol0mka
    Commented Aug 3, 2020 at 18:33
7

You can set up a really good alias that can handle this without the overly verbose syntax.

I have the following alias in ~/.gitconfig:

po = "!git push -u origin \"$(git rev-parse --abbrev-ref HEAD)\""

After making a commit on a new branch, you can push your new branch by simply typing the command:

git po
3
  • why po? push origin? what happen if this is ran multiple times?
    – Arnold Roa
    Commented Aug 20, 2017 at 21:20
  • Yes, as in push origin. Nothing happens if it's run multiple times. I also have a git push -f alias set up to git pf, so I use that once the origin has already been pushed.
    – 123
    Commented Aug 20, 2017 at 21:57
  • see djanowski's comment, you can directly use HEAD
    – arhak
    Commented Oct 10, 2017 at 8:46
6

I did something similar to a lot of other users but wanted to share it as an alternative since I didn't see anyone else post this.

alias gpu='git push --set-upstream origin $(git branch --show-current)'

(oh-my-zsh already has a gpu alias so edited that in .oh-my-zsh/plugins/git/git.plugin.zsh)

0
5

For those looking for an alias that works with git pull, this is what I use:

alias up="git branch | awk '/^\\* / { print \$2 }' | xargs -I {} git branch --set-upstream-to=origin/{} {}"

Now whenever you get:

$ git pull
There is no tracking information for the current branch.
...

Just run:

$ up
Branch my_branch set up to track remote branch my_branch from origin.
$ git pull

And you're good to go

0
4

99% of the time I want to set the upstream to a branch of the same name, so I use this (in *nix or Git Bash):

git branch --set-upstream-to=origin/$(git branch --show-current)

It's nice because it's branch agnostic. Note the sub-command git branch --show-current prints your current branch name, or nothing if you are detached.

Side note: I have my config set such that I can use git push -u, so I rarely need to do this. But I still do sometimes, and it's usually when I decide I want to reset my local changes to whatever's on the remote, and at that moment I realize I previously pushed without -u. So, typically the next command I'm going to run after setting my upstream, is resetting to the remote branch:

git reset --hard @{u}

Which also happens to be branch agnostic. (Maybe I just really dislike typing in my branch name.)

4
  • Got error: the requested upstream branch 'origin/test' does not exist Commented Sep 26, 2022 at 11:44
  • @VitalyZdanevich that means you haven't pushed your branch yet. (This question was about setting your upstream after you already pushed the branch.) In your case you can set the upstream on the first push, something like git push --set-upstream origin test
    – TTT
    Commented Sep 26, 2022 at 14:22
  • But can I set upstream without push/pull? I want to have an alias that will create a new branch and set the upstream, currently I have this broken code: [alias] cr = "!f() { git switch -c $1; git branch --set-upstream-to=origin/master $1; }; f" Commented Sep 26, 2022 at 14:54
  • @VitalyZdanevich I don't know of a good way to say "this is what I will want to track once I push it." I suppose you could update your alias to push it out at branch creation with the upstream set, though it might be odd to announce a new branch name with no new commits on it yet, but at least it would accomplish your goal...
    – TTT
    Commented Sep 26, 2022 at 15:27
2

Because git has the cool ability to push/pull different branches to different "upstream" repositories. You could even use separate repositories for pushing and pulling - on the same branch. This can create a distributed, multi-level flow, I can see this being useful on project such as the Linux kernel. Git was originally built to be used on that project.

As a consequence, it does not make assumption about which repo your branch should be tracking.

On the other hand, most people do not use git in this way, so it might make a good case for a default option.

Git is generally pretty low-level and it can be frustrating. Yet there are GUIs and it should be easy to write helper scripts if you still want to use it from the shell.

1

We use phabricator and don't push using git. I had to create bash alias which works on Linux/mac

vim ~/.bash_aliases

new_branch() {
    git checkout -b "$1"
    git branch --set-upstream-to=origin/master "$1"
}

save

source ~/.bash_aliases
new_branch test #instead of git checkout -b test
git pull
1

Here is a bash alias for git push which is safe to run for every push and will automatically switch between setting upstream for the first push and then doing normal pushes after that.

alias gpu='[[ -z $(git config "branch.$(git symbolic-ref --short HEAD).merge") ]] && git push -u origin $(git symbolic-ref --short HEAD) || git push'

Original Post

1
  • Got error: the requested upstream branch 'origin/test' does not exist Commented Sep 26, 2022 at 15:05
1

All i wanted was doing something like this:

git checkout -b my-branch
git commit -a -m "my commit"
git push

Since i didn't found a better solution, i've just created an bash alias on ~/.bashrc:

alias push="git push -u origin HEAD"

now just doing a push command does the job (you can add this alias on ~/.gitconfig too with another name, such as pushup)

0

I sort of re-discovered legit because of this issue (OS X only). Now all I use when branching are these two commands:

legit publish [<branch>] Publishes specified branch to the remote. (alias: pub)

legit unpublish <branch> Removes specified branch from the remote. (alias: unp)

SublimeGit comes with legit support by default, which makes whole branching routine as easy as pressing Ctrl-b.

0

There are a lot of good answers here, however, all of them require you to do something else correctly before running git pull

It certainly helps to have aliases that do things like "make git push work the way it ought to, by creating a remote branch that the local is properly tracking". However, none of that helps you when you forget to use them, or went through a different workflow.

Here is a bash function that you can use to do a pull the way it ought to work, by detecting when you don't have a remote merge target configured, but there is a branch on the remote with the same name as your local branch, and setting that branch as the merge target, then pulling.

git-pulldown() {
    head="$(git rev-parse --abbrev-ref HEAD)"

    if [[ $(git config "branch.$head.merge") ]]; then #there's already a merge target configured, just pull as normal from there
        git pull
    else
        if [[ $(git ls-remote --heads origin $head) ]]; then #there is an upstream branch existing with the same name as our branch
            git branch --set-upstream-to origin/$head #set merge target to upstream branch with same name
            git pull
        else #fail with explanation
            echo "Branch $head has no upstream or merge target! You will likely have to push first, or manually configure it"
            return 1
        fi
    fi
}
-1

There is apparently no supported way to override default options of git commands. Based on this answer to Define git alias with the same name to shadow original command, we can override the behavior of git push in bash to always call git push -u. Put the following in your ~/.bash_profile file, and it should be equivalent to running --set-upstream every time you push.

function do_git {
  cmd=$1
  shift
  myArgs=( "$@" )

  if [ "$cmd" == "push" ]; then
    myArgs=( "-u" "${myArgs[@]}" )
  fi
  myArgs=( "$cmd" "${myArgs[@]}" )

  $(which git) "${myArgs[@]}"
}
alias  git='do_git'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.