[英]Git: When squashing or rebasing in a feature branch, why do you need to compare it to another branch like master?
我有一個功能分支feature_account
。 它是2個月前從master
分支出來的。 我在此分支中進行了多次提交,但我仍想在功能分支中將它們全部壓縮為1次提交。
顯然, master
已經獨立於功能分支進行了更改。 從我的功能分支中,如果我輸入git rebase -i master
,那么我會從僅屬於master的更改中得到合並沖突---如果我只想合並來自Feature分支的所有提交,為什么master中的內容很重要成1次提交? 我不想擔心師父,直到我嘗試並入師父。 難道我做錯了什么?
假設提交圖是這樣的,
* b73f9ac (HEAD -> feature_account) f
* 5e77f54 e
* f6e5be0 d
| * c96a113 (master) c
| * f8132ea b
|/
* bae8722 a
* 762077a initial commit
在git rebase -i master
,新的基礎是master
。 但是在您的情況下,新的基址應該是bae8722
,這是分叉點和最近的公共祖先。 可以通過git merge-base feature_account master
。 在某些更復雜的情況下, git merge-base
可能無法正常工作,您需要通過其他方式找到它。
要顯式指定新的基准,可以使用--onto <newbase>
,
git rebase -i --onto $(git merge-base feature_account master) master
或簡寫形式
git rebase -i --onto feature_account...master master
然后在編輯器中,將除第一個以外的其他行上的“ pick”更改為“ s”。 保存並退出。 編輯提交消息並完成。
要feature_account
上的新提交,還有另一種方法。
git checkout feature_account
# make the working tree clean if it's not
git stash -u
# here "feature_account...master" cannot be used
git reset $(git merge-base feature_account master) --soft
git commit
# restore the stashed changes if necessary
git stash apply --index
假設您對feature_account
分支進行了三次提交。
當您輸入git rebase -i master
您要求git嘗試在master
當前指向的提交之上重播這三個提交。 您有可能修復沖突三遍(每次您重新定基提交一次)。
您已表明希望將feature_account
為一次提交。 一種方法是找到您開始工作的提交,並將其用作git rebase -i <commit>
。 使用三個提交,此命令可能是: git rebase -i HEAD^^^
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.