[英]Git: How to rebase to a specific commit in past?
我想重新定位到一個特定的提交,它不是另一個分支的 HEAD,而是倒退:
A --- B --- C master
\
\-- D --- E topic
至
A --- B --- C master
\
\-- D --- E topic
我如何以優雅和通用的方式實現這一目標?
一般來說,我的意思是目標提交 (B) 可能不一定是 HEAD 的直接祖先(我不妨變基為 A 或以前的提交),並且主題分支上可能不僅僅是兩個提交。 我可能還想從 B 變基到 A。
使用 git 櫻桃挑選
git rebase
是git cherry-pick
類固醇。
如果您只有幾個提交要移動:您可以使用git cherry-pick
來一一挑選
# move back to B
git checkout B
# start a branch here, and use it as the active branch
git checkout -b wip
# cherry-pick the commits you want to have
git cherry-pick D
git cherry-pick E
# if all went well : move the 'topic' branch to the current branch :
git checkout topic
git reset --hard wip
# delete the temporary 'wip' branch
git branch -d wip
使用 git 變基
正如評論中提到的: git rebase
可以采用額外的選項來僅移動一系列提交。
您可以使用以下方法執行與上述cherry-pick
序列相同的操作:
git rebase --onto B master topic
額外說明:git rebase -i
git rebase
也有一個--interactive|-i
標志:
git rebase -i --onto B master topic
使用-i
標志:在執行任何操作之前,git 將為您打開一個文本編輯器,其中將列出所有重新定位的提交。
此步驟的明顯好處是向您展示將應用的內容(在實際應用之前),並且還可以描述更多的操作,而不僅僅是“選擇那個提交”。
您可以搜索 web 以獲取有關git rebase -i
的更完整教程,這里有一個:
Git 交互式 Rebase、Squash、Amend 和其他重寫歷史的方式 (thoughtbot.com)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.