2017-06-08 85 views
2

如果我創建了一個新分支並嘗試推送它,我被告知必須明確說明上游分支的名稱應該是什麼。如何設置上游分支與分支的名稱相同

> git checkout -b feature/long-branch-name-I-dont-want-to-have-to-type-out 
Switched to a new branch 'feature/long-branch-name-I-dont-want-to-have-to-type-out' 
> git push 
fatal: The current branch feature/long-branch-name-I-dont-want-to-have-to-type-out has no upstream branch. 
To push the current branch and set the remote as upstream, use 

    git push --set-upstream origin feature/long-branch-name-I-dont-want-to-have-to-type-out 

有沒有辦法做到這一點,而不必輸入上游分支的名稱?我實際上總是希望它在服務器上與本地名稱相同。

有什麼辦法可以做到像git push --set-upstream <current_branch_name>這樣的東西,而不管當前分支名稱是什麼?

回答

2

[編輯] sajib khan's first answer, setting push.default to current將啓用推送,但不實際設置上游。這意味着在未來git fetch之後,你的Git不會報告提前/落後計數,你的Git將不知道上游用於git rebasegit merge(或者git pull或者我建議避開git pull)。

您可以使用[編輯,在他回答的第二部分]:

git push -u origin HEAD 

如果需要的話,這造成對其他的Git分支,讓你的Git獲取origin/變種。然後,無論如何,它會將您(分支)的遠程追蹤分支設置爲分支的上游分支。但直到origin/feature/long-branch-name-I-dont-want-to-have-to-type-out實際上存在,您不能將其設置爲上游。


其實,你可以,你就不能使用git branch --set-upstream做到這一點。而且,你不想再次輸入它。要做到「手動」,你需要:

git config \ 
    branch.feature/long-branch-name-I-dont-want-to-have-to-type-out.remote origin 
git config \ 
    branch.feature/long-branch-name-I-dont-want-to-have-to-type-out.merge \ 
    feature/long-branch-name-I-dont-want-to-have-to-type-out 

這意味着輸入三次(!),或寫自己的腳本。

1

配置混帳配置

$ git config --global push.default current 

現在,結賬到分支後,你應該使用簡單git push

$ git checkout -b new-branch 
$ git push     # similar to git push -u origin new-branch 

您還可以使用HEAD而不是鍵入當前分支的名字。

$ git push -u origin HEAD 

N.B. HEAD和本地當前分支通常保持相同的狀態。

相關問題