limp_chimp
limp_chimp

Reputation: 15163

Automatically track remote branch with git

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.

Upvotes: 373

Views: 138575

Answers (4)

Loren
Loren

Reputation: 12711

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

Run to update your configuration:

git config --global --type 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.

Upvotes: 776

Richard
Richard

Reputation: 2625

Here is an example of setting this in your .gitconfig file:

[init]
    defaultBranch = main
[pull]
    rebase = false
[push]
    # Automatically set origin as the remote for current branch
    autoSetupRemote = true

Note: I excluded other sections in the config file for simplicity.

PS, it would be really cool to see a config reference will all the options. I found this git manual from MIT, git-config(1): https://web.mit.edu/git/www/git-config.html

Upvotes: 1

Ivaylo Strandjev
Ivaylo Strandjev

Reputation: 70929

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.

Upvotes: 64

torek
torek

Reputation: 488183

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).

Upvotes: 24

Related Questions