243

When I'm using a local branch mybranch, I'd like to be able to push to and pull from origin mybranch using just git push and git pull. As it is, I have to tediously write out git push origin mybranch and git pull origin mybranch. If I try to use just git pull for example, I get:

There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details

    git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

    git branch --set-upstream-to=origin/<branch> mybranch

And if I enter git branch --set-upstream-to=origin/mybranch mybranch, then it works. But this is almost as tedious as the previous commands. Can I just have git do this as default behavior? I've seen similar questions asked and the answers tend to suggest that newer versions of git do this, but I'm using git version 2.1.3, which is fairly new, so it can't just be that.

limp_chimp
  • 13,475
  • 17
  • 66
  • 105
  • 4
    Next time you push, use `git push -u origin mybranch`. From then on, when you're on `mybranch`, `git push` and `git pull` will be enough. – jub0bs Apr 02 '15 at 20:55
  • 22
    With Git 2.37 (Q3 2022), a [`git config --global push.autoSetupRemote`](https://stackoverflow.com/a/72401899/6309) can help. – VonC May 29 '22 at 10:32
  • Don't use `--global` unless you know what you are doing. You can get hard to find bugs a year later when you have long forgotten that you once used `--global`. – Martin Aug 04 '22 at 14:48
  • 3
    or always use global, in that way you know all your repository behave the same – Jonatan Cloutier May 31 '23 at 15:39

4 Answers4

507

As of git 2.37.0, this is now possible with git configuration.

Run to update your configuration:

git config --global --add --bool push.autoSetupRemote true

Then git push will automatically setup the remote branch.

Note: The --global flag means this will apply to all git commands on your machine (regardless of which repo it is), you can omit the flag to make it specific to a single repo on your machine.

Documentation:

https://git-scm.com/docs/git-config#Documentation/git-config.txt-pushautoSetupRemote

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.

Loren
  • 9,783
  • 4
  • 39
  • 49
  • 3
    I find it dangerous to tell to use `--global` without telling the user that they change the configuration for **all** repositories on the machine. – Martin Aug 04 '22 at 14:52
  • 8
    I added an explanation about global. – Loren Aug 04 '22 at 17:29
  • 8
    Why the `--add` flag? Can this setting have multiple options? And `--bool` isn't needed in this case either. – Qtax Oct 11 '22 at 13:57
  • `git version 2.39.1` on my mac does not seem to take above into effect; when i run 'git pull' it still asks me to set tracking information. – soMuchToLearnAndShare Mar 06 '23 at 10:33
  • You have to set the config and have pushed that branch first (which sets up the remote). – Loren Mar 07 '23 at 13:14
58

One possible solution is to modify your git push behavior to current (note that this will also have some other side affects). You can do that either by modifying your ~/.gitconfig directly (as a file and this assumes you are on Linux) or by executing:

git config --global push.default current

Now when you push, git will automatically push your current branch to a remote branch with the same name even if you don't specify it explicitly. In addition if your current branch does not have a remote equivalent, a remote branch will be created and your current branch will be set to track it (same as git push -u origin new_branch but with a single git push). Also have a look at this question where the git push behavior is described in detail.

Iulian Onofrei
  • 9,188
  • 10
  • 67
  • 113
Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
24

It's not github, it's your local git, that does all this.

If you are creating mybranch locally with git checkout and it already exists as origin/mybranch in your local repository, you can simply git checkout mybranch and your local git will see that origin/mybranch exists and create local mybranch with origin/mybranch as its upstream.

On the other hand, if origin/mybranch does not exist yet and you're creating mybranch locally with git checkout -b or similar, you can't really set it to track the upstream branch that does not exist yet (you can configure it to track that branch but you'll get an occasional complaint that the upstream version is not there).

In this particular case, on the first push (the one that will create the branch upstream), you can use:

git push -u origin mybranch

which tells your local git to push mybranch to origin and, once that's done, set up origin/mybranch as the tracking-branch for mybranch.

Note that this is the same as running git branch --set-upstream-to: you do it once, after which it's set locally and you don't have to do it again. It's just more convenient since you can do it together with the push that creates the branch at origin. But you still have to remember to do it (once; you'll get reminded when you run git push without -u origin mybranch).

torek
  • 448,244
  • 59
  • 642
  • 775
  • Ah, I thought I had avoided referencing github specifically but I typo'd. Will fix. Thanks for the info! I think I'll just make a bash alias to do this when I create the branch. – limp_chimp Apr 02 '15 at 23:01
-1

git -u origin HEAD is one way to not have to write out the entire name of your branch.

-u is shorthand for --set-upstream and HEAD refers to the name of your current head (the branch you're currently on)

triple
  • 1
  • 1
  • This does not address the question. The question asked for a way to automate the process (which is possible with a config option, described by other answers), not for a shorthand. – amycodes Jun 05 '23 at 17:15
  • @amycodes Stack Overflow didn't let me add this as a follow-up comment to the other response about `git push -u origin mybranch` because I don't have 50 reputation, so I had to reply as a comment to OP instead. – triple Jun 05 '23 at 19:33
  • Unfortunately you will just have to wait till 50 rep. I too think the system is flawed, but that is the way it is. Answers should not be used as comments. – amycodes Jun 05 '23 at 20:12