综述
Git 设置本地分支对应的远程分支的两类方法
# Set upstream when pushing to remote
git push -u origin test
# Set upstream without pushing it
# with option -u / --set-upstream-to
git branch -u origin/test
git branch --set-upstream-to=origin/test
复制代码
git push
可以在本地分支在第一次push时设置远程分支。可以通过选项-u或——set-upstream为每个更新或推送的分支设置远程引用。例如,我的本地仓库checkout一个分支test,此时提交到git仓库时,可以使用
$ git push -u origin test
复制代码
git branch
如果你不想push,只是单纯的想设置本地分支对应的远程分支,可以通过下面命令实现
git branch -u origin/test
Branch 'test' set up to track remote branch 'test' from 'origin'.
复制代码
或者
git branch --set-upstream-to=origin/test
Branch 'test' set up to track remote branch 'test' from 'origin'.
复制代码
注意,使用git branch 的时候,如果远程分支不存在,则会提示报错:
>git branch -u origin/test
error: the requested upstream branch 'origin/test' does not exist
hint:
hint: If you are planning on basing your work on an upstream
hint: branch that already exists at the remote, you may need to
hint: run "git fetch" to retrieve it.
hint:
hint: If you are planning to push out a new local branch that
hint: will track its remote counterpart, you may want to use
hint: "git push -u" to set the upstream config as you push.
复制代码
提示信息中说明了,需要使用git push 去设置远程分支。
一旦设置好了,可以通过下面命令查看git的分支对应情况
git branch -vv
* test a1304b7 [origin/test] bug fix
full 665c405 [origin/full] fix unit test bug
master 21230b8 [origin/master: behind 20] Merge branch 'test' into 'master'
复制代码
取消设置
$ git branch --unset-upstream [<branchname>]
复制代码