1. 程式人生 > >git rebase -i 修改提交

git 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儲存修改。

$ git add sample.txt
$ git 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之前的狀態。

from: http://backlogtool.com/git-guide/cn/stepup/stepup7_6.html