Make an existing Git branch track a remote branch?
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.Make an existing Git branch track a remote branch?
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.
官方文檔:git-branch(1) Manual Page
-u <upstream>
--set-upstream-to=<upstream>
Set up <branchname>'s tracking information
so <upstream> is considered <branchname>'s upstream branch.
If no <branchname> is specified, then it defaults to the current branch.
假定澡为,需要把foo分支涂身,和遠(yuǎn)端的foo分支進(jìn)行映射识颊,遠(yuǎn)端名稱為upstream
如果當(dāng)foo分支不是當(dāng)前分支,使用如下命令
git branch -u upstream/foo foo
如果foo分支是當(dāng)前分支
git branch -u upstream/foo
==============================================================================
在checkout分支的時(shí)候件炉,就進(jìn)行映射
he ProGit book has a very good explanation:
Tracking Branches
Checking out a local branch from a remote branch automatically creates what is called a tracking branch. Tracking branches are local branches that have a direct relationship to a remote branch. If you’re on a tracking branch and type git push, Git automatically knows which server and branch to push to. Also, running git pull while on one of these branches fetches all the remote references and then automatically merges in the corresponding remote branch.
When you clone a repository, it generally automatically creates a master branch that tracks origin/master. That’s why git push and git pull work out of the box with no other arguments. However, you can set up other tracking branches if you wish — ones that don’t track branches on origin and don’t track the master branch. The simple case is the example you just saw, running git checkout -b [branch] [remotename]/[branch]. If you have Git version 1.6.2 or later, you can also use the --track shorthand:
$ git checkout --track origin/serverfix
Branch serverfix set up to track remote branch refs/remotes/origin/serverfix.
Switched to a new branch "serverfix"
To set up a local branch with a different name than the remote branch, you can easily use the first version with a different local branch name:
$ git checkout -b sf origin/serverfix
Branch sf set up to track remote branch refs/remotes/origin/serverfix.
Switched to a new branch "sf"
Now, your local branch sf will automatically push to and pull from origin/serverfix.
查看本地分支和遠(yuǎn)端分支的映射情況git branch -vv
查看映射
Administrator@LuJunTao MINGW64 /d/SourceCode/GameSourceCode/Hearthstone-Deck-Tracker (chucklu_zhCN)
$ git branch -vv
chucklu_master 8739b35 [chucklu/master] v0.10.16 (mainwindow topmost fix)
- chucklu_zhCN d5dbcc8 1.修改默認(rèn)語(yǔ)言為中文zhCN
master 8739b35 [epix37/master] v0.10.16 (mainwindow topmost fix)
ui-translation cade481 [epix37/ui-translation] a few more resx entries
發(fā)現(xiàn)chucklu_zhCN分支沒有和遠(yuǎn)端建立映射關(guān)系
添加映射
Administrator@LuJunTao MINGW64 /d/SourceCode/GameSourceCode/Hearthstone-Deck-Tracker (chucklu_zhCN)
$ git branch -u chucklu/chucklu_zhCN
Branch chucklu_zhCN set up to track remote branch chucklu_zhCN from chucklu.
再次查看映射
Administrator@LuJunTao MINGW64 /d/SourceCode/GameSourceCode/Hearthstone-Deck-Tracker (chucklu_zhCN)
$ git branch -vv
chucklu_master 8739b35 [chucklu/master] v0.10.16 (mainwindow topmost fix)
- chucklu_zhCN d5dbcc8 [chucklu/chucklu_zhCN: ahead 1] 1.修改默認(rèn)語(yǔ)言為中文zhCN //可以發(fā)現(xiàn)這個(gè)已經(jīng)映射成功了
master 8739b35 [epix37/master] v0.10.16 (mainwindow topmost fix)
ui-translation cade481 [epix37/ui-translation] a few more resx entries官方文檔:git-branch(1) Manual Page
-u <upstream>
--set-upstream-to=<upstream>
Set up <branchname>'s tracking information
so <upstream> is considered <branchname>'s upstream branch.
If no <branchname> is specified, then it defaults to the current branch.
假定,需要把foo分支诞挨,和遠(yuǎn)端的foo分支進(jìn)行映射碍粥,遠(yuǎn)端名稱為upstream
如果當(dāng)foo分支不是當(dāng)前分支,使用如下命令
git branch -u upstream/foo foo
如果foo分支是當(dāng)前分支
git branch -u upstream/foo
==============================================================================
在checkout分支的時(shí)候,就進(jìn)行映射
he ProGit book has a very good explanation:
Tracking Branches
Checking out a local branch from a remote branch automatically creates what is called a tracking branch. Tracking branches are local branches that have a direct relationship to a remote branch. If you’re on a tracking branch and type git push, Git automatically knows which server and branch to push to. Also, running git pull while on one of these branches fetches all the remote references and then automatically merges in the corresponding remote branch.
When you clone a repository, it generally automatically creates a master branch that tracks origin/master. That’s why git push and git pull work out of the box with no other arguments. However, you can set up other tracking branches if you wish — ones that don’t track branches on origin and don’t track the master branch. The simple case is the example you just saw, running git checkout -b [branch] [remotename]/[branch]
. If you have Git version 1.6.2 or later, you can also use the --track
shorthand:
$ git checkout --track origin/serverfix
Branch serverfix set up to track remote branch refs/remotes/origin/serverfix.
Switched to a new branch "serverfix"
To set up a local branch with a different name than the remote branch, you can easily use the first version with a different local branch name:
$ git checkout -b sf origin/serverfix
Branch sf set up to track remote branch refs/remotes/origin/serverfix.
Switched to a new branch "sf"
Now, your local branch sf
will automatically push to and pull from origin/serverfix
.
查看本地分支和遠(yuǎn)端分支的映射情況git branch -vv
查看映射
Administrator@LuJunTao MINGW64 /d/SourceCode/GameSourceCode/Hearthstone-Deck-Tracker (chucklu_zhCN)
$ git branch -vv
chucklu_master 8739b35 [chucklu/master] v0.10.16 (mainwindow topmost fix)
- chucklu_zhCN d5dbcc8 1.修改默認(rèn)語(yǔ)言為中文zhCN
master 8739b35 [epix37/master] v0.10.16 (mainwindow topmost fix)
ui-translation cade481 [epix37/ui-translation] a few more resx entries
發(fā)現(xiàn)chucklu_zhCN分支沒有和遠(yuǎn)端建立映射關(guān)系
添加映射
Administrator@LuJunTao MINGW64 /d/SourceCode/GameSourceCode/Hearthstone-Deck-Tracker (chucklu_zhCN)
$ git branch -u chucklu/chucklu_zhCN
Branch chucklu_zhCN set up to track remote branch chucklu_zhCN from chucklu.
再次查看映射
Administrator@LuJunTao MINGW64 /d/SourceCode/GameSourceCode/Hearthstone-Deck-Tracker (chucklu_zhCN)
$ git branch -vv
chucklu_master 8739b35 [chucklu/master] v0.10.16 (mainwindow topmost fix)
- chucklu_zhCN d5dbcc8 [chucklu/chucklu_zhCN: ahead 1] 1.修改默認(rèn)語(yǔ)言為中文zhCN //可以發(fā)現(xiàn)這個(gè)已經(jīng)映射成功了
master 8739b35 [epix37/master] v0.10.16 (mainwindow topmost fix)
ui-translation cade481 [epix37/ui-translation] a few more resx entries