我已經 fork 了一個 github 存盤庫,現在我的 fork 已經有幾個提交了。我現在想將其中一個提交作為 PR 提供給原始存盤庫。
按照這個問題的答案,我做了:
git remote add official [URL to original repo]
git checkout -b hotfix-for-feature official/master
git cherry-pick [feature-hash]
git push -u origin hotfix-for-feature
我得到:
! [rejected] hotfix-for-feature -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/myusername/repositoryname'
hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart. Check out this branch and integrate the remote changes
hint: (e.g. 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
注意如何:
- 的分支引數
git push -u被完全忽略,它想推送到原點/主控 - 既不遠
origin/hotfix-for-feature也不official/hotfix-for-feature存在。 official/master無論如何都沒有領先 - 我真的只是把它拿來了
如果我嘗試git branch hotfix-for-feature --set-upstream=origin/hotfix-for-feature,我被告知這git push -u是正確的方法。如果我嘗試git push -u origin/hotfix-for-feature,我會得到:
fatal: You are pushing to remote 'origin/hotfix-for-feature',
which is not the upstream of your current branch 'hotfix-for-feature',
without telling me what to push to update which remote branch.
哪個 - 除了是如何不寫錯誤訊息的一個很好的例子 - 我不明白。我指定要推送的內容(當前分支)和要更新的遠程分支。
我發現很多關于這個錯誤的問題,但它總是關于一些遠程分支在前面,在這種情況下沒有現有的遠程分支。“推送分支”和“遠程[分支]”之間還有什么區別?
uj5u.com熱心網友回復:
您收到以下錯誤訊息:
! [rejected] hotfix-for-feature -> master (non-fast-forward) error: failed to push some refs to 'https://github.com/myusername/repositoryname' hint: Updates were rejected because a pushed branch tip is behind its remote hint: counterpart. Check out this branch and integrate the remote changes hint: (e.g. 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.
其中重要的部分是
hotfix-for-feature -> master
您正在嘗試將本地分支推hotfix-for-feature送到遠程分支master。
git push -u 的分支引數被完全忽略,它想要推送到 origin/master
它沒有被忽視。它將本地分支推hotfix-for-feature送到遠程分支master。
說明檔案git push意味著使用refspec:
指定要使用什么源物件更新的目標參考。引數的格式是可選的加號
,后跟源物件,后跟冒號:,后跟目標 ref。...
在第二個引數中,您不僅可以指定要推送的(本地)分支,還可以指定要推送到的(遠程)分支(git push <remote> <sourcebranch>:<destinationbranch>)。
知道了這一點,您就可以git push -u origin hotfix-for-feature:hotfix-for-feature將本地分支推hotfix-for-feature送到遠程分支hotfix-for-feature。
uj5u.com熱心網友回復:
TL;DR:不要創建你的分支official/master
當您首先創建分支時,首先結帳official/master,然后git checkout -b hotfix-for-feature不指定應該從什么創建分支。
您也可以這樣做git checkout -b hotfix-for-feature master,但明確地說git checkout -b hotfix-for-feature official/master以您不想要的方式設定上游。
細節
這個問題一開始讓我很驚訝,因為我經常做一個與你幾乎相同的作業流程,但默認情況下git push -u origin newbranchname會newbranchname在origin.
所以,我試著看看你的作業流程和我的作業流程有什么不同,我只是想通了。
當你跑的時候:
git checkout -b hotfix-for-feature official/master
您特別要求 Git 創建 branch hotfix-for-featureto track official/master,這意味著您已經完成了-u前期作業,并且設定錯誤。
在我的作業流程中,我改為這樣做:
git checkout official/master
git checkout -b hotfix-for-feature
它創建了一個沒有上游的新分支。
然后,當我使用與您相同的語法推送此分支時,上游現在設定為您想要的,在您的場景中您自己的 fork 上:
git push -u origin hotfix-for-feature
hotfix-for-feature為我的本地分支創建origin和設定。origin/hotfix-for-featurehotfix-for-feature
或者,您從本地分支創建新分支,這也不會設定上游:
git checkout -b hotfix-for-feature master
這只是official/master你必須避免的最后一個論點。
uj5u.com熱心網友回復:
我個人不同意其他答案。(我覺得跟蹤具有不同名稱的遠程分支會令人困惑,而且我不喜歡查看共享分支的本地副本。)我會在您的作業流程中進行的唯一調整是創建分支。我會改變這一行:
git checkout -b hotfix-for-feature official/master
不跟蹤遠程分支,如下所示:
git checkout -b hotfix-for-feature official/master --no-track
請注意,當您從遠程分支創建新的本地分支時,一些 UI 工具會為您執行此操作。例如,在 Visual Studio 中,默認情況下選中“跟蹤遠程分支”復選框,直到您將分支名稱更改為其他名稱,在這種情況下,它會自動為您取消選中該框(并--no-track在幕后添加)。
提示:如果您忘記使用--no-track,您可以在事后使用:
git branch --unset-upstream
這也將解決您當前的問題,之后您可以進行初始推送以git push -u正確設定上游。
旁注:我也可能會調整有問題的同一行以使用更新的結帳語法:
git switch -c hotfix-for-feature official/master --no-track
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/430623.html
