簡體   English   中英

如何獲取git以按名稱自動匹配遠程分支?

[英]How can I get git to automatically match a remote branch by name?

我經常使用克隆的存儲庫,有一個“原始”(我)和“上游”(原始資源)。 我總是從github上克隆“原始”存儲庫以進行處理並創建PR,並且有時需要從“上游”中提取信息以與正在進行的最新更改保持同步。

git clone <origin uri>我可以做

git push/pull

由於分支已經被跟蹤,所以不指定分支; 但是在另一個遙控器上執行此操作

git pull upstream

當例如在master分支上我想git實際上

git pull upstream master

代替:

You asked to pull from the remote 'upstream', but did not specify
a branch. Because this is not the default configured remote
for your current branch, you must specify a branch on the command line.

我知道我可以配置跟蹤遙控器,但是我想知道只要分支名稱相同,它是否可以自動進行。

您要求從遠程“上游”拉出,但未指定分支。 因為這不是當前分支的默認配置遠程服務器,所以必須在命令行上指定一個分支。

查看消息,第一件事是您尚未將upstream配置為默認遙控器。 因此,首先,您需要將其設置為默認遙控器,例如:

git config branch.branch_name.remote upstream

現在,您需要指定一個要從中提取提交的分支。 您可以通過運行來指定

git config branch.branch_name.merge refs/heads/branch_name

希望能幫助到你。

只需使用--track--set-upstream-to(-u)

$ git branch --track master upstream/master

對於當前分支:

$ git branch -u upstream/master
$ git branch --set-upstream-to=upstream/master

這會將遠程跟蹤分支upstream/master分配給您的master分支,以便在下一個git pull調用中自動獲取它。

從手冊頁:

   -t, --track
       When creating a new branch, set up branch.<name>.remote and branch.<name>.merge
       configuration entries to mark the start-point branch as "upstream" from the new
       branch. This configuration will tell git to show the relationship between the 
       two branches in git status and git branch -v. Furthermore, it directs git pull
       without arguments to pull from the upstream when the new branch is checked out.

   -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.

您可以使用以下配置,因此,每當您從遠程簽出新分支時,都會自動對其進行跟蹤,而無需使用--set-upstream-to--track設置跟蹤

git config --global branch.autosetupmerge true

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM