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?
By the way, the shortcut to pushing the current branch to a remote with the same name:
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):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!
You can use:
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 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.
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:
Note: '-t' is the shortened version of '--track' option.
This sets up the same association right off the bat.