轉自:http://www.backlogtool.com/git-guide/cn/stepup/stepup7_6.html
用rebase -i 修改提交
爲了節省時間,這個教程使用現有的歷史記錄作爲本地數據庫。
從這裏下載
我們進入stepup-tutorial/tutorial6目錄。本地端的歷史記錄的狀態如下圖顯示。我們在這裏修改「添加commit的講解」的內容。
數據庫的歷史記錄
用rebase -i ,首先選擇要修改的提交。
$ git rebase -i HEAD~~
打開文本編輯器,將看到從HEAD到HEAD~~的提交如下圖顯示。
pick 9a54fd4 添加commit的說明
pick 0d4a808 添加pull的說明
Rebase 326fc9f..0d4a808 onto d286baa
#
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
#
If you remove a line here THAT COMMIT WILL BE LOST.
However, if you remove everything, the rebase will be aborted.
#
將第一行的“pick”改成“edit”,然後保存並退出。將會顯示以下內容,修改過的提交呈現退出狀態。
Stopped at d286baa… 添加commit的說明
You can amend the commit now, with
git commit --amend
Once you are satisfied with your changes, run
git rebase --continue
打開sample.txt,適當地修改“commit的講解”部分。
連猴子都懂的Git命令
add 把變更錄入到索引中
commit 記錄索引的狀態
pull 取得遠端數據庫的內容
用commit –amend保存修改。
現在已經commit,但是rebase操作還沒結束。若要通知這個提交的操作已經結束,請指定 –continue選項執行rebase。
$ git rebase –continue
Note
這時,有可能其他提交會發生衝突, 請修改衝突部分後再執行add和rebase –continue。這時不需要提交。如果在中途要停止rebase操作,請在rebase指定–abort選項執行,這樣就可以抹去並停止在rebase的操作。
提交的修改完成了。如果要把多個提交修改成edit,下一個要修改的提交會退出,請執行同樣的修改。
Note
實際上,在rebase之前的提交會以ORIG_HEAD之名存留。如果rebase之後無法復原到原先的狀態,可以用git reset –hard ORIG_HEAD復原到rebase之前的狀態。
刪除中間的commit,只需要在編輯框,將pick (commit id)這句話移除即可