2014-09-13 118 views
0

在我的git倉庫中,我只有包含8個提交的主分支。 在提交2中,我插入了一個錯誤的類,現在我會在提交2中以這種方式重寫git歷史記錄。我沒有錯誤的類,因此下一個提交沒有提交。 什麼是正確的方法來做到這一點?編輯git歷史

在此先感謝。

+1

'git rebase -i' – 2014-09-13 15:42:54

+0

rebase我需要編輯此提交的代碼。我如何做到這一點?之後,這個編輯好的代碼會傳播到下一個提交? – 2014-09-13 15:50:43

+1

是的,rebase中的一個選項將允許你在提交時進入提交模式,刪除類然後重做提交。未來的提交將是不同於原始的提交對象。如你所要求的那樣,重寫歷史。就我個人而言,我只需要堅持下去,而不必擔心它,除非這個提交是像發行版本號這樣的開發中的一個重要階段。 – 2014-09-13 15:55:08

回答

1

您可以使用此:git rebase -i HEAD~7,這將允許您interactively rebase在當前HEAD之前的最後6次提交。

默認情況下,這將打開一個預先填充了一些說明文本和最近7個提交ID的文本編輯器。基本上可以按照指示,並改變相應的不正確的行承諾開始edit

pick f7f3f6d whatever 
edit a5f4a0d did something wrong 
pick 310154e added foobar.junk and whatever.html 
pick d332af2 something else 
pick 7e2f413 yadda yadda 
... 

# 
# Commands: 
# p, pick = use commit 
# r, reword = use commit, but edit the commit message 
# e, edit = use commit, but stop for amending 
# s, squash = use commit, but meld into previous commit 
# f, fixup = like "squash", but discard this commit's log message 
# x, exec = run command (the rest of the line) using shell 
# 
# These lines can be re-ordered; they are executed from top to bottom. 
# 
# If you remove a line here THAT COMMIT WILL BE LOST. 
# 
# However, if you remove everything, the rebase will be aborted. 
# 
# Note that empty commits are commented out 

後保存並退出,您的索引將被倒回到它剛剛經過git commit荷蘭國際集團的國家提交你正在編輯。

然後,您應該編輯它以糾正它(例如,git commit --amend),並且還可以在其上添加新的提交。完成所有工作後,使用它繼續並重播剩餘的(較新的)提交:git rebase --continue

請注意,最後一行可能觸發合併衝突;我假設你已經知道如何處理這些:)

+2

小修改 - 編輯提交時Git在提交後停止,而不是之前。在它停止之前必須執行'git reset HEAD ^'之前得到它。 – 2014-09-15 01:48:09

+1

謝謝安德魯,我糾正了這一點。這是我「做的」事情之一,並沒有考慮到究竟發生了什麼......哦,哦。 – 2014-09-15 01:51:04