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 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:
git branch --set-upstream foo upstream/foo
Notes:
All of the above commands will cause local branch foo
to track remote branch foo
from remote upstream
. 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.
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
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 --set-upstream
.
The git-branch
command also understands --set-upstream
, but its use can be confusing. Version 1.8.0 modifies the interface.
git branch --set-upstream
is 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
or just
$ git branch --set-upstream-to=origin/foo foo
链接地址: http://www.djcxy.com/p/76.html
上一篇: 你如何创建一个远程Git分支?
下一篇: 让现有的Git分支跟踪远程分支?