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?
When you set your upstream (or tracking) branches, you can simply execute pulls and pushes without having to specify the target branch. Git automatically knows that it has to fetch the new commits to the remote tracking branch. Similarly, Git already knows that it has to push new commits to the upstream branch.
When the current branch i.e ('new_branch') has no Upstream branch set and we try to run the command “Git push”. After running the below command in cmd: Now, you need to set the upstream branch using the Git push command with the -u option.
The git set-upstream allows you to set the default remote branch for your current local branch. By default, every pull command sets the master as your default remote branch.
Set Upstream If you don't want to push anything, you can also do it using git-branch command. A local branch can track a remote branch using git-branch with long option --set-upstream-to=<upstream> or short option -u <upstream> . The command sets up branchname 's tracking information.
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 branchorigin/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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With