需要認識到的一點是,rebase不會重寫歷史記錄或移動提交,在Git中的提交無法更改。相反,它創造了新的歷史,並一直這樣說。例如,當你開始:
4-5-6(feature)
|
1-2-3(master)
|
7(bugfix)
然後git rebase bugfix feature
到底發生了什麼是這樣的:
4-5-6
|
1-2-3(master)
|
7(bugfix)-4A-5A-6A(feature)
三個新提交製成,4A,5A和6A。原始提交仍然存在,但沒有指向它們。他們最終會被清理乾淨,但他們會在那裏待上好幾天。
這意味着你可以撤銷rebase,這就是你正在做的。你需要找到feature
就在rebase之前。這可以通過git reflog
完成,每次跟蹤HEAD
移動。這發生在checkout
,commit
,reset
和rebase
。 git reflog
可能是這樣的:
65e93ca (HEAD -> feature) [email protected]{0}: rebase finished: returning to refs/heads/feature
65e93ca (HEAD -> feature) [email protected]{1}: rebase: 3 feature
6d539a3 [email protected]{2}: rebase: 2 feature
3cd634f [email protected]{3}: rebase: 1 feature
b84924b (bugfix) [email protected]{4}: rebase: checkout bugfix
a9fd2f1 [email protected]{5}: commit: 3 feature
29136bc [email protected]{6}: commit: 2 feature
60543b0 [email protected]{7}: commit: 1 feature
c487530 (master) [email protected]{8}: checkout: moving from master to feature
這告訴我a9fd2f1是在要素最後提交它被重訂了。我可以將特徵恢復,而不是重做rebase。
git checkout feature
git reset --hard a9fd2f1
在未來,如果你做底墊之前git tag
功能的原始位置這樣的事情是由一個容易得多。然後,您可以將git reset
返回到該標籤,而無需搜索reflog。
至於你的具體問題,問題是,重訂後你的資料庫,現在看起來是這樣的:
6A [feature]
|
5A
|
4A
|
7 [bugfix]
|
3 [master]
|
2
|
1
當你問git rebase master feature
GIT中指出,主已經是功能的祖先和不沒有。錯誤修正是介於兩者之間並不重要。
取而代之,您需要告訴Git您要重新綁定4A,5A和6A並忽略7.這是使用--onto
語法完成的。
git rebase --onto master bugfix feature
這就是說要從基礎上修補,但不包括修補功能到母版上。
我會推薦使用git reset
而不是試圖重做rebase。不能保證第二次兌換會出現相同的情況,尤其是在發生衝突的情況下。與git reset
相反,您正在顯式地回到存儲庫的舊狀態。
謝謝! git rebase --onto正是我所需要的。 git reflog會工作,如果我沒有做任何修改後修復bugfix。 – chohocvo