git 修改倒数二个提交

2021-12-16 17:29:03 浏览数 (4)

之前有介绍 commit --amend , 通过这个命令可以修改最新的commit提交。

还不了解的可以查看git 修改最后一次commit 文章

那如果想修改的是倒数第二个commit提交,应该怎么办呢?

先说结论

通过rebase -i 命令 rebase -irebase --interactive 的缩写。

操作实践

  1. 找到要修正的commit. 假如 倒数第二个 commit e47fa58590d449b83c1a48352a295a20ce3105aa 是错误的提交
代码语言:javascript复制
$ git log -3
commit 338955c322f09349d3a9a9207314aa16c51396d1 (HEAD -> branch_test)
Author: 大龙
Date:   Tue Oct 22 20:22:42 2019  0800

    提交12

commit e47fa58590d449b83c1a48352a295a20ce3105aa
Author: 大龙
Date:   Tue Oct 22 20:22:30 2019  0800

    提交11

commit 7f83da39cd4542450782812e3ee55a7e511b2e30
Author: 大龙
Date:   Tue Oct 22 20:22:21 2019  0800

    提交10
  1. 使用 rebase -i 命令修改倒数第二个commit

开启编辑页面,修改要操作的那次commit, 这里修改的是 e47fa58 提交11 这次提交

tips:

edit: 使用本次提交,在rebase到这次提交时候,会暂停下来等待修正

pick:使用本次提交,不操作修改

drop:删除这次提交

...

可进行的操作,编辑界面都有列出来

代码语言:javascript复制
$ git rebase -i HEAD~2

edit e47fa58 提交11
pick 338955c 提交12

# Rebase 7f83da3..338955c onto 7f83da3 (2 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# .       create a merge commit using the original merge commit's
# .       message (or the oneline, if no original merge commit was
# .       specified). Use -c <commit> to reword the commit message.
#
# 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
~                                             
  1. 退出编辑界面,显示如下提示信息
代码语言:javascript复制
Stopped at e47fa58...  提交11
You can amend the commit now, with

  git commit --amend 

Once you are satisfied with your changes, run

  git rebase --continue
  1. 修改 commit ,完成后提交修改。 可以看到倒数第二个commit 已经被修改掉了。
代码语言:javascript复制
$ git add .
$ git commit --amend
  1. 继续rebase流程
代码语言:javascript复制
$ git rebase --continue 

6.查看下结果:

代码语言:javascript复制
$ git log -3
commit cc6c6d0deaa111ccfaadf4573ccef90edf083dc1 (HEAD -> branch_test)
Author: 大龙
Date:   Tue Oct 22 20:22:42 2019  0800

    提交12

commit 80da970b6931c1cbdf269d5d553d5da57788aa36
Author: 大龙
Date:   Tue Oct 22 20:22:30 2019  0800

    提交11 ament

commit 7f83da39cd4542450782812e3ee55a7e511b2e30
Author: 大龙
Date:   Tue Oct 22 20:22:21 2019  0800

    提交10

tip:

rebase -i命令也可以用来删除指定的一次commit

END!

1 人点赞