6. 用rebase -i 修改提交

    为了节省时间,这个教程使用现有的历史记录作为本地数据库。

    从这里下载

    我们进入stepup-tutorial/tutorial6目录。本地端的历史记录的状态如下图显示。我们在这里修改「添加commit的讲解」的内容。

    用rebase -i ,首先选择要修改的提交。

    1. pick 9a54fd4 添加commit的说明
    2. pick 0d4a808 添加pull的说明
    3.  
    4. # Rebase 326fc9f..0d4a808 onto d286baa
    5. #
    6. # Commands:
    7. # r, reword = use commit, but edit the commit message
    8. # e, edit = use commit, but stop for amending
    9. # s, squash = use commit, but meld into previous commit
    10. # f, fixup = like "squash", but discard this commit's log message
    11. # x, exec = run command (the rest of the line) using shell
    12. #
    13. # However, if you remove everything, the rebase will be aborted.
    14. #

    将第一行的“pick”改成“edit”,然后保存并退出。将会显示以下内容,修改过的提交呈现退出状态。

    打开sample.txt,适当地修改“commit的讲解”部分。

    1. 连猴子都懂的Git命令
    2. add 把变更录入到索引中
    3. commit 记录索引的状态
    4. pull 取得远端数据库的内容

    用commit —amend保存修改。

    现在已经commit,但是rebase操作还没结束。若要通知这个提交的操作已经结束,请指定 —continue选项执行rebase。

      Note

      提交的修改完成了。如果要把多个提交修改成edit,下一个要修改的提交会退出,请执行同样的修改。

      Note

      实际上,在rebase之前的提交会以ORIG_HEAD之名存留。如果rebase之后无法复原到原先的状态,可以用git reset —hard ORIG_HEAD复原到rebase之前的状态。

      下一页