git rebase 修改历史提交

文章转载自:6. 用rebase -i 修改提交【教程3 改写提交!】| 猴子都能懂的GIT入门 | 贝格乐(Backlog)

本地端的历史记录的状态如下图显示。我们在这里修改「添加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之前的状态。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值