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
Editing .git/config
is probably the easiest and fastest way. That's what the Git commands for handling remote branches are doing, anyway.
If you don't want to muck with the file by hand (and it's not that hard to do), you can always use git config
to do it...but again, that's just going to edit the .git/config
file, anyway.
There are, of course, ways to automatically track a remote branch when using git checkout
(by passing the --track
flag, for example), but these commands work with new branches, not existing ones.
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 a grb
command to explain what git commands it would execute.
grb explain create my_branch github
# git_remote_branch version 0.3.0
# List of operations to do to create a new remote branch and track it locally:
git push github master:refs/heads/my_branch
git fetch github
git branch --track my_branch github/my_branch
git checkout my_branch
For git version 2.25.1
, use the command:
git push --set-upstream origin <local_branch_name>
This would work too
git branch --set-upstream-to=/< remote>/< branch> < localbranch>
or simply by :
switch to the branch if you are not in it already:
[za]$ git checkout branch_name
run
[za]$ git branch --set-upstream origin branch_name
Branch origin set up to track local branch brnach_name by rebasing.
and you ready to :
[za]$ git push origin branch_name
You can alawys take a look at the config file to see what is tracking what by running:
[za]$ git config -e
It's also nice to know this, it shows which branches are tracked and which ones are not. :
[za]$ git remote show origin
For creating new branch, we could use following command
git checkout --track -b example origin/exampleFor the already created branch to create link between remote then from that branch use below command
git branch -u origin/remote-branch-name