修改倒数第二个提交记录

40

如果你在上次提交中发现了一个错误,你可以使用以下命令重新提交上次的提交:

git commit --amend

但如果你已经提交了另一个commit,那么你如何重新提交该commit之前的commit?


注意: 假设你已经确认在上游没有其他开发人员使用你的提交,你才应该使用--amend命令。


1
如何修改指定的提交? - rubo77
可能是如何修改指定的提交?的重复问题。 - user456814
4个回答

36
  1. 使用以下命令提交你的修复:

    git commit -a -m "fix commit(此提交将向上移动一行)"

    (提交信息不重要,完成后它将过时)

  2. 现在是魔法时间:
    将 HEAD 变基到倒数第二个提交,但在此之前编辑提交信息,使其交换最后两行

    git rebase -i HEAD~3

    编辑器将显示最后 3 次提交,如下所示:

     pick 2a06f16 this was the last commit
     pick 0dbc5ce the last-but-one commit
     pick 2e30418 fix commit (this one will be shifted up one line)
    # Rebase 011f3d0..2e30418 onto 011f3d0 # …

    交换最后两行,使提交信息看起来像这样:

     pick 2a06f16 this was the last commit
     fixup 2e30418 fix commit (this one will be shifted up one line)
     pick 0dbc5ce the last-but-one commit
    # Rebase 011f3d0..2e30418 onto 011f3d0 # …
  3. 使用以下命令检查日志:

    git log HEAD~3
  4. 使用+将更正的提交行强制推送到已存在的分支:

    git push origin +branchname

3
有一个更短的方法来完成同样的操作:使用git commit -a --fixup=HEAD^(或要修复的任何提交),然后运行git rebase -i HEAD~3,您无需更改任何内容,只需关闭编辑器即可,git将自动处理修复操作。 - aragaer
与您的答案相同,不同之处在于“--fixup”只是自动化第二步。 - aragaer
1
你能提供另一个答案吗?使用你的版本(你可以复制粘贴我的答案并添加你的更改)。 - rubo77
@aragaer:你的评论在我的Ubuntu上不起作用。我将其创建为答案,是否有遗漏的内容? - rubo77
1
你在第三步实际上是指 git log -3 吗?我不明白如何使用你提供的命令来检查我的日志。 - iron9

24
嗯,我在搜索同样的内容时,找到了这个页面。发现了更好的方法,还有许多其他选择。
git rebase -i HEAD~2

对于VIM编辑器:

一个编辑器将会打开,显示以下细节

pick 4f4f96f Added git ignore
pick d01e18c Added sample Blog

# Rebase 60e1cd3..d01e18c onto 60e1cd3 (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>]

按下i键,将pick更改为rreword,类似下面这样
pick 4f4f96f Added git ignore
r d01e18c Added sample Blog

按下 esc + : + wq 另一个窗口将打开,更改提交信息
按下 esc + : + wq 对于记事本或任何其他编辑器:
将其作为普通文本文件进行编辑(选择 r 或 reword)并保存。
最后
git push -f 

这绝对是最容易的一个。同时熟悉“rebase -i”是一个好习惯,因为它在不同的情况下非常强大。 - Stuck
1
我使用了这种方法在倒数第二个提交的提交消息中添加问题编号。非常简单和快速。谢谢! - Siddhesh T
这会添加/修改暂存文件还是仅仅重新编辑提交信息? - ruffin
只是想补充一下,如果你想重命名倒数第二个提交,你需要在第一行前面写上 r,像这样 'r 4f4f96f Added git ignore'。 - Abdur Rafay
请注意,末尾的指示仅适用于VIM编辑器。如果您将编辑器切换为Windows上的Notepad++之类的编辑器,只需按照平常的方式使用它来将命令更改为"e"/"edit"(如果您想完全修改,而不仅仅是改写)或"r"/"reword"(仅改写)放在您想要更改的提交旁边。同时,请确保在更改命令后保存并关闭文件。 - undefined

5
@aragaer的评论是实现同样效果的更简短的方法:
git commit -a --fixup=HEAD^ #(or whatever commit to be fixed)

那么。
git rebase -i HEAD~3

你不需要进行任何更改,只需关闭编辑器,git 就会自动处理 fixup。

注意:这在我的 Ubuntu 系统上无法使用!


你是Windows用户吗?如果不是,你仍然需要编辑那个文件;只有Git for Windows在交互式变基期间特别处理“fixup”提交。 - Edward Thomson
请随意编辑我的答案,我只是想将aragaers的评论作为答案添加。如果这行不通,我会接受我自己更长的答案作为正确答案。我使用Linux,无法在Windows上测试。 - rubo77

0
在 Git GUI 中,您可以轻松实现此操作:
  1. 在最新的提交中添加一个名为 temp 的分支
  2. 将分支 master 重置到要编辑的提交之前的提交
  3. 逐个挑选来自 temp 分支的所有提交并将其应用到 master,如果需要可以编辑提交信息
  4. 使用 + 强制推送更正后的提交行以强制将其推送到已存在的分支上

    git push origin +master
    

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接