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.
Given a branch foo and a remote upstream:
As of Git 1.8.0:
git branch -u upstream/foo
Or, if local branch foo is not the current branch:
git branch -u upstream/foo foo
Or, if you like to type longer commands, these are equivalent to the above two:
git branch --set-upstream-to=upstream/foo
git branch --set-upstream-to=upstream/foo foo
As of Git 1.7.0 (before 1.8.0):
git branch --set-upstream foo upstream/foo
Notes:
foo to track remote branch foo from remote upstream.git fetch upstream beforehand.See also: Why do I need to do `--set-upstream` all the time?
You can do the following (assuming you are checked out on master and want to push to a remote branch master):
Set up the 'remote' if you don't have it already
git remote add origin ssh://...
Now configure master to know to track:
git config branch.master.remote origin
git config branch.master.merge refs/heads/master
And push:
git push origin master
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
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.
For git version 2.25.1, use the command:
git push --set-upstream origin <local_branch_name>
In case you got "error: the requested upstream branch 'origin/foo' does not exist" after running:
git branch -u origin/foo
Make sure origin does have a foo branch.
Make sure the remote.origin.fetch variable is set to +refs/heads/*:refs/remotes/origin/*:
$ git config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'
$ git config --get remote.origin.fetch
+refs/heads/*:refs/remotes/origin/*
git fetch -v. You should see git updating from origin/foo:$ git fetch -v
From github.com:davidhcefx/test
* [new branch] foo -> origin/foo
= [up to date] master -> origin/master
git branch -avv will show square brackets, indicating tracked remote branches:$ git branch -u origin/foo
branch 'foo' set up to track 'origin/foo'.
$ git branch -avv
* foo 92c5ada [origin/foo] Initial commit
master 92c5ada [origin/master] Initial commit
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 anyone who, like me, just wants to sync up your local branch name with the remote branch name, here's a handy command:
git branch -u origin/$(git rev-parse --abbrev-ref HEAD)
To avoid remembering what you need to do each time you get the message:
Please specify which branch you want to merge with. See git-pull(1)
for details.
.....
You can use the following script which sets origin as upstream for the current branch you are in.
In my case I almost never set something else than origin as the default upstream. Also I almost always keep the same branch name for local and remote branch. So the following fits me:
#!/bin/bash
# scriptname: git-branch-set-originupstream
current_branch="$(git branch | grep -oP '(?<=^\* )(.*)$')"
upstream="origin/$current_branch"
git branch -u "$upstream"