I know how to make a new branch that tracks remote branches, but how do I make an existing branch track a remote branch?
I know I can just edit the .git/config
file, but it seems there should be an easier way.
I know how to make a new branch that tracks remote branches, but how do I make an existing branch track a remote branch?
I know I can just edit the .git/config
file, but it seems there should be an easier way.
You might find the
git_remote_branch
tool useful. It offers simple commands for creating, publishing, deleting, tracking & renaming remote branches. One nice feature is that you can ask agrb
command to explain what git commands it would execute.Here, using
github
andgit version 2.1.4
, just do:And remotes come by itelsef, even if not linked locally:
But of course, still no local branch:
See? Now if you just checkout develp, it will do the magic automatically:
So easy!
Summary. Just run this 2 commands:
I do this as a side-effect of pushing with the
-u
option as inThe equivalent long option is
--set-upstream
.The
git-branch
command also understands--set-upstream
, but its use can be confusing. Version 1.8.0 modifies the interface.Say you have a local
foo
branch and want it to treat the branch by the same name as its upstream. Make this happen withor just
This isn't a direct answer to this question, but I wanted to leave a note here for anyone who may be having the same issue as me when trying to configure an upstream branch.
Be wary of push.default.
With older git versions, the default was matching, which would cause very undesirable behaviour if you have, for example:
Local branch "master" tracking to origin/master
Remote branch "upstream" tracking to upstream/master
If you tried to "git push" when on the "upstream" branch, with push.default matching git would automatically try to merge the local branch "master" into "upstream/master", causing a whole lot of chaos.
This gives more sane behaviour:
git config --global push.default upstream
For creating new branch, we could use following command
For the already created branch to create link between remote then from that branch use below commandActually for the accepted answer to work: