Git 設置遠程分支

綜述

Git 設置本地分支對應的遠程分支的兩類方法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倉庫時,可使用bash

$ git push -u origin test
複製代碼

git branch

若是你不想push,只是單純的想設置本地分支對應的遠程分支,能夠經過下面命令實現fetch

git branch -u origin/test
Branch 'test' set up to track remote branch 'test' from 'origin'.

複製代碼

或者ui

git branch --set-upstream-to=origin/test
Branch 'test' set up to track remote branch 'test' from 'origin'.

複製代碼

注意,使用git branch 的時候,若是遠程分支不存在,則會提示報錯:spa

>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 去設置遠程分支。code

一旦設置好了,能夠經過下面命令查看git的分支對應狀況rem

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>]
複製代碼
相關文章
相關標籤/搜索