[英]Fixing detached head on a renamed git branch
git的新功能。 我有一個功能分支( feature/123
),該分支最初是從develop
剪切下來的,在使用后又被推送到GitHub。
我剛剛開始對該分支進行一些本地更改,然后我決定要保留所做的更改(而不是將其全部丟棄),並從GitHub當前提供的分支版本重新開始。
因此,我決定:
feature/123
分支(GitHub) 所以我:
git add .
git commit -m "Saving changes made thus far"
git branch -m feature/123-OLD
git checkout origin/feature/123
當我這樣做時,我看到:
$ git checkout origin/feature/123
Note: checking out 'origin/feature/123'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:
git checkout -b <new-branch-name>
HEAD is now at 8877c28... 123: did some stuff
現在,當我查看當前分支時,我看到:
$ git branch
* (HEAD detached at origin/feature/123)
開發
所有這些“超脫頭”的東西都讓我毛骨悚然!
我想要的是擁有一個名為feature/123
的本地分支,該分支與GitHub的狀態完全相同。 我該怎么做?
這里的前三個命令都很不錯:
git add . git commit -m "Saving changes made thus far" git branch -m feature/123-OLD
最后一個將名稱feature/123
更改為名稱feature/123-OLD
(當然,首先假設您在一個名為feature/123
的分支上)。
最后一條命令沒有錯 ,這不是您想要的命令:
git checkout origin/feature/123
您想要的是:
git checkout feature/123
這會環顧四周你的(本地)分支名稱, 無法找到名為什么feature/123
(不存在任何更多- feature/123-OLD
存在,但是這是不一樣的),然后調用“做什么,我的意思“ git checkout
功能。
這個功能說: 嗯,我找不到您想要的分支。 我想也許您對我來說意味着要創建一個新分支! 我是否可以找到一個看起來很像feature/123
遠程跟蹤名稱,例如origin/feature/123
feature/123
? 如果找不到任何一個或兩個或更多,我將抱怨並失敗。 但是,如果我能找到一個 ,我將創建一個新 feature/123
,指向與遠程跟蹤名稱相同的提交!
當然,它只會找到一個,因此它將創建feature/123
。 當確實創建feature/123
,它將默認將其上游設置為origin/feature/123
。 這就是“按我的意思做”或DWIM checkout
方式,實際上就是您的意思。
您可以結帳所需的分支
git checkout feature/123
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.