Make an existing Git branch track a remote branch?
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
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
- All of the above commands will cause local branch
footo track remote branch
- The old (1.7.x) syntax is deprecated in favor of the new (1.8+) syntax. The new syntax is intended to be more intuitive and easier to remember.
- Defining an upstream branch will fail when run against newly-created remotes that have not already been fetched. In that case, run
git fetch upstreambeforehand.
Read more... Read less...
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
git push origin master
I do this as a side-effect of pushing with the
-u option as in
$ git push -u origin branch-name
The equivalent long option is
git-branch command also understands
--set-upstream, but its use can be confusing. Version 1.8.0 modifies the interface.
git branch --set-upstreamis deprecated and may be removed in a relatively distant future.
git branch [-u|--set-upstream-to]has been introduced with a saner order of arguments.
It was tempting to say
git branch --set-upstream origin/master, but that tells Git to arrange the local branch "origin/master" to integrate with the currently checked out branch, which is highly unlikely what the user meant. The option is deprecated; use the new
--set-upstream-to(with a short-and-sweet
-u) option instead.
Say you have a local
foo branch and want it to treat the branch by the same name as its upstream. Make this happen with
$ git branch foo $ git branch --set-upstream-to=origin/foo
$ git branch --set-upstream-to=origin/foo foo
For Git versions 1.8.0 and higher:
Actually for the accepted answer to work:
git remote add upstream <remote-url> git fetch upstream git branch -f --track qa upstream/qa # OR Git version 1.8.0 and higher: git branch --set-upstream-to=upstream/qa # Gitversions lower than 1.8.0 git branch --set-upstream qa upstream/qa
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
I believe that in as early as Git 1.5.x you could make a local branch
$BRANCH track a remote branch
origin/$BRANCH, like this.
origin/$BRANCH exist, and you've not currently checked out
$BRANCH (switch away if you have), do:
git branch -f --track $BRANCH origin/$BRANCH
$BRANCH as a tracking branch. The
-f forces the creation despite
$BRANCH existing already.
--track is optional if the usual defaults are in place (that is, the git-config parameter
branch.autosetupmerge is true).
origin/$BRANCH doesn't exist yet, you can create it by pushing your local
$BRANCH into the remote repository with:
git push origin $BRANCH
Followed by the previous command to promote the local branch into a tracking branch.