相信大家都知道 git 下面的这个命令了:

git commit --amend

通过这个命令可以修改最新的 commit 提交。也就是指向当前 HEAD 的那次提交。但是,如果想修改的是倒数第二次 commit 提交而又不想惊动倒数第一次的 commit 提交,应该怎么办呢?

git rebase 就像一块臭豆腐,没吃之前闻起来好臭,吃过以后发现“真香”。不过话说回来,git rebase 也算是 git 里边的高级操作了,下来我们就来看看是怎么用它来修改倒数第二个 commit 的吧!

起因

任何东西都有个因果缘由,我写这篇博客的原因也是。

某天,我修改了程序文件的某个功能,因为逻辑上是两个东西,所以对这两次修改分别提交了两次 commit,然后 gmail 出去了,结果被 committer 指出我第一次的 commit 提交有问题,还需要进行修改,但是第二次提交应该是没有问题的。

[root@master GitTest]# git log
commit a892d9e31d85c2d37eb036d3ef89197a7bceaacb (HEAD -> master)
Author: looking <looking@qq.com>
Date:   Mon Aug 31 21:29:24 2020 +0800nice to meet you toocommit 155cd2e87152c5f60a2421651a86690f7cd984c6
Author: looking <looking@qq.com>
Date:   Mon Aug 31 21:16:51 2020 +0800nice to meet youcommit 3f2061298d921378da14f4003753f1f277e67243 (origin/master)
Author: looking <looking@qq.com>
Date:   Thu Aug 20 23:02:46 2020 +0800I am Lookingadd one line in hello.txtSigned-off-by: Lu Kaiyi <looking@qq.com>

那么?现在该怎么办?我有以下几个选择。

第一个选择

git reset --hard HEAD^1

由于本地提交了两次而且还没有 Applied 到远端,所以领先了远端 2 个 commit。

[root@master GitTest]# git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.(use "git push" to publish your local commits)nothing to commit, working tree clean

既然要修改倒数第二个 commit,那我就把 HEAD 切换过那去,修改文件,暂存文件,重新提交 commit:

[root@master GitTest]# git reset --hard HEAD^1
HEAD is now at 155cd2e nice to meet you
[root@master GitTest]# vim hello.txt
[root@master GitTest]# git add hello.txt
[root@master GitTest]# git commit --amend

这样做似乎没什么问题,毕竟倒数第二次 commit 的 bug 已经成功修复。但是,我倒数第一次的 commit 和对文件的修改都丢失了,现在不得不重新提交一遍,真 TMD * 蛋(心中暗暗骂了一句)。所以,这只能算是不怎么地的一个可选方案了。

第二个选择

git reset HEAD~

你可能想,我只撤销倒数第一次的 commit,不撤销倒数第一次的文件修改,不还是能回到倒数第二次的 commit 去!

[root@master GitTest]# git reset HEAD~
Unstaged changes after reset:
M   hello.txt
[root@master GitTest]# git log
commit 3d879227e4fe5f1091ec431a767308edb3059e21 (HEAD -> master)
Author: looking <looking@qq.com>
Date:   Mon Aug 31 21:16:51 2020 +0800nice to meet youcommit 3f2061298d921378da14f4003753f1f277e67243 (origin/master)
Author: looking <looking@qq.com>
Date:   Thu Aug 20 23:02:46 2020 +0800I am Lookingadd one line in hello.txtSigned-off-by: Lu Kaiyi <looking@qq.com>

但是你看下边,commit 虽然已经撤销了,但是文件还是被追踪到为 modified,如果两次是对不同文件 commit 的话其实还行(毕竟这种情况你至少可以使用 git add file 单独添加文件到暂存区)。但是如果你倒数第二次和倒数第一次 commit 修改的是同一个文件的内容,岂不又 GG 了,毕竟现在可能没办法做到只 commit 文件的一部分修改(没说太绝,怕打脸)。还有一点就是我倒数第一个 commit 提交写的 comment 内容实在太多了,现在都没了,我还得重新写一遍 commit comment,还是有点不爽快:

[root@master GitTest]# git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.(use "git push" to publish your local commits)Changes not staged for commit:(use "git add <file>..." to update what will be committed)(use "git restore <file>..." to discard changes in working directory)modified:   hello.txtno changes added to commit (use "git add" and/or "git commit -a")

第三个选择

git revert <commit-id>

git revert 是用于“反做”某一个版本,以达到撤销该版本的修改的目的。 其特点是会新建一个 commit 来回滚之前 commit-id 的操作,可以看到 git revert <commit-id> 执行的操作是 commit-id 操作的逆操作,也就是 commit-id 做的动作,它都会反向操作一遍。这样操作以后,你倒数第二个 commit 的提交就被撤销了,这时候你就可以继续修改再次提交了。

root@master ~/GitTest# git revert 72bfd90b8194b1d3537fc62a22d6776d32ae5b4b
[master 780b3e0] Revert "add hello in hello.txt"1 file changed, 1 deletion(-)
root@master ~/GitTest# git show
commit 780b3e0044b10aa62a6459a44d4b657da76a501d (HEAD -> master)
Author: looking <looking@qq.com>
Date:   Mon Jun 6 17:26:12 2022 +0800Revert "add hello in hello.txt"This reverts commit 72bfd90b8194b1d3537fc62a22d6776d32ae5b4b.diff --git a/hello.txt b/hello.txt
index 25f7272..83585f1 100644
--- a/hello.txt
+++ b/hello.txt
@@ -1,4 +1,3 @@
-hellonicehello worldhello world. I am Looking

当然, 如果倒数第一个 commit 的修改是基于倒数第二个 commit 基础之上的修改,那么很可能也会冲突的,这时候就需要自己手动处理一下冲突了。

修改冲突的文件
git add 冲突的文件
git revert --continue

最好的选择

git rebase -i HEAD~2 和 git rebase --continue

[root@master GitTest]# git log
commit a892d9e31d85c2d37eb036d3ef89197a7bceaacb (HEAD -> master)
Author: looking <looking@qq.com>
Date:   Mon Aug 31 21:29:24 2020 +0800nice to meet you toocommit 155cd2e87152c5f60a2421651a86690f7cd984c6
Author: looking <looking@qq.com>
Date:   Mon Aug 31 21:16:51 2020 +0800nice to meet youcommit 3f2061298d921378da14f4003753f1f277e67243 (origin/master)
Author: looking <looking@qq.com>
Date:   Thu Aug 20 23:02:46 2020 +0800I am Lookingadd one line in hello.txtSigned-off-by: Lu Kaiyi <looking@qq.com>

git rebase -i HEAD~2

git rebase -i HEAD~2 以后,git 会自动给你切换到下面这个界面,你将你需要修改的那个 commit 前边的 pick 修改为 edit。

[root@master GitTest]# git rebase -i HEAD~2edit 3d87922 nice to meet you
pick df5f952 nice to meet you too# Rebase 3f20612..df5f952 onto 3f20612 (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.

然后保存退出,而且 git 也已经提醒你版本已经 stopped 在这个 commit 节点了,你可以开始你的表演(修改)了:

[root@master GitTest]# git rebase -i HEAD~2
Stopped at 3d87922...  nice to meet you
You can amend the commit now, withgit commit --amend Once you are satisfied with your changes, rungit rebase --continue

假如我想把原来插入的 nice to meet you 修改为 nice。用 vim 修改后用 git commit --amend 重新提交 commit。

[root@master GitTest]# vim hello.txt
[root@master GitTest]# git add hello.txt
[root@master GitTest]# git commit --amendnice # Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Mon Aug 31 21:16:51 2020 +0800
#
# interactive rebase in progress; onto 3f20612[detached HEAD bcbfd91] niceDate: Mon Aug 31 21:16:51 2020 +08001 file changed, 1 insertion(+)

git rebase --continue

然后继续 git rebase --continue 就好了(如果没有冲突,直接 git rebase --continue 也是极好的)。

[root@master GitTest]# git add hello.txt
[root@master GitTest]# git rebase --continue
[detached HEAD 72017cd] nice to meet you too1 file changed, 1 insertion(+)
Successfully rebased and updated refs/heads/master.

如果是对同一个文件修改的话,还是需要自己处理冲突的,不过至少把你当前的提交和最后一次提交的冲突反映到了文件。处理完成以后,重新对冲突文件进行 git add file 来表示你已经处理好冲突了,然后再次执行 git rebase --continue 就大功告成(git 已经帮你把最后一次 commit comment 保存起来的,所以不用担心重新写提交 comment 的问题啦) 。

[root@master GitTest]# git rebase --continue
Auto-merging hello.txt
CONFLICT (content): Merge conflict in hello.txt
error: could not apply df5f952... nice to meet you too
Resolve all conflicts manually, mark them as resolved with
"git add/rm <conflicted_files>", then run "git rebase --continue".
You can instead skip this commit: run "git rebase --skip".
To abort and get back to the state before "git rebase", run "git rebase --abort".
Could not apply df5f952... nice to meet you too[root@master GitTest]# git add hello.txt
[root@master GitTest]# git rebase --continue
[detached HEAD 72017cd] nice to meet you too1 file changed, 1 insertion(+)
Successfully rebased and updated refs/heads/master.

最后查看日志和文件内容,已经成功修改。

[root@master GitTest]# git log
commit 72017cd6e0222ff5fb544ce460ad3f2b046952ed (HEAD -> master)
Author: looking <looking@qq.com>
Date:   Mon Aug 31 21:59:19 2020 +0800nice to meet you toocommit 50a084c8cce985ba6b5ee9bb7bce7a950671bb8e
Author: looking <looking@qq.com>
Date:   Mon Aug 31 21:16:51 2020 +0800nicecommit 3f2061298d921378da14f4003753f1f277e67243 (origin/master)
Author: looking <looking@qq.com>
Date:   Thu Aug 20 23:02:46 2020 +0800I am Lookingadd one line in hello.txt

git rebase --abort

如果你在 rebase 的任何过程中想退出 rebase 过程,直接执行 git rebase --abort 就直接退出回到 rebase 之前的状态啦。

git 修改倒数二个 commit相关推荐

  1. git分支合并、撤销;git修改已push的commit信息; git 撤销操作;

    git分支合并 1.分支代码提交 2.git branch 查询本地分支 3.git checkout 分支名1 切换分支到需要合并的分支上 4.git merge 分支名2  //选择要合并到 分支 ...

  2. 【Git】Git 修改刚提交的 commit message

    Git 修改 commit message 修改最近一次的commit 信息 git commit --amend 然后就会进入vim编辑模式 比如要修改的commit是倒数第三条,使用命令: git ...

  3. git 修改已提交的 commit

    2019独角兽企业重金招聘Python工程师标准>>> 修改历史的操作,原理上都是通过变基(rebase)实现的. 因为发生了修改,则每个涉及的 commit 都会计算出新的 SHA ...

  4. Git修改已提交的commit

    1 本地修改 由于以下修改本身是对版本历史的修改,在需要push到远程仓库时,往往是不成功的,只能强行push,这样会出现的一个问题就是,如果你是push到多人协作的远程仓库中,会对其他人的远程操作构 ...

  5. Git 修改已提交的 commit 信息

    一.步骤 1.git log --oneline -5 查看最近5次commit的简要信息,输出信息为:简短commitID commit_message,可以根据需要查看最近n次的提交也可以git ...

  6. git修改提交作者邮箱

    git config user.name "现在的名字" git config user.email "现在的邮箱" 1.有限次修改 a) 修改最后一次 git ...

  7. git修改提交的commits信息

    有时候我们可能需要修改以前的提交代码过程中的commits信息,下面我就做个简单的例子 1. 确定要修改的commits是倒数第几次提交的 git log查看提交的commits,找到你需要更改的co ...

  8. GIT 修改commit message

    背景:很多时候,我们项目对提交的message有格式要求,如果我们git commit提交之后,格式不正确,那就无法push,这时候需要修改提交的message 1.修改本次提交message git ...

  9. Git 修改commit 相关操作

    修改最新提交的commit的message git commit --amend 执行 git commit --amend 然后修改注释即可 (需要通过vi 或vim编辑并保存) 修改旧的commi ...

最新文章

  1. python-day22(序列化)
  2. angular具体用法及代码
  3. mysql 批量加索引_mysql优化:按期删数据 + 批量insert + 字符串加索引为何很傻
  4. 《从入门到精通云服务器》-4
  5. 随想录(编写简单资源管理代码)
  6. 如何在矩池云GPU云中安装MATLAB R2017b软件
  7. 转载:Windows Phone 7 资源汇总(超全)
  8. Ext 3.1版本放出,可以免费下载了
  9. 楼梯计算机方法,怎么计算楼梯踏步方法是什么
  10. 基于springboot高校社团管理系统
  11. ppt压缩文件怎么压缩?
  12. 小马激活工具出现Cannot open file k:\OEMSF 的解决方法
  13. C语言 输出1000年~2000年之间的闰年
  14. PDF怎么转换成jpg图片
  15. Django应用容器封装DockerFile分享
  16. 使用ctex宏包出现的kpathsea错误
  17. csdn 群发 粉丝 博文 博客
  18. 火柴人生存挑战2html5游戏在线玩,火柴人生存挑战
  19. 抖音SEO优化源码,企业号搜索排名系统,矩阵分发。
  20. 充电器pps功能是什么_联想推出45W拯救者充电器,支持PPS功能主打便携

热门文章

  1. 安全计算:使用ClamWin为高级用户提供免费病毒防护
  2. 各品牌手机进rec快捷键
  3. echarts 动态数据动画效果
  4. proble tip
  5. Java MD5加密解密
  6. 八个步骤教你做好会议现场管理
  7. 古罗马数字的基本知识
  8. 符合Chrome58的证书制作
  9. 海外社交媒体SNS运营之社交媒体概览
  10. riak教程 java_riak-java-client