修改倒数第二个提交

时间:2014-07-08 06:52:21

标签: git

如果您在上一次提交中发现了错误,则只需使用

重新提交最后一次提交即可
git commit --amend

但是如果您已经提交了另一个提交,那么如何在该提交之前重新提交提交?


注意:我认为,如果您确定没有其他开发人员已使用您的上游提交,那么您应该只使用--amend

4 个答案:

答案 0 :(得分:7)

  1. 使用

    提交修正
    git commit -a -m "fix commit (this one will be shifted up one line)"

    (提交信息并不重要,一旦完成就会过时)

  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

答案 1 :(得分:3)

@aragaer的评论甚至更短的方式:

git commit -a --fixup=HEAD^ #(or whatever commit to be fixed)

然后

git rebase -i HEAD~3

并且您不需要更改任何内容,因此您可以关闭编辑器,git将自行处理修复。

注意:这不适用于我的Ubuntu系统!

答案 2 :(得分:0)

在git GUI中,您可以轻松实现以下目标:

  1. temp分支添加到最新提交
  2. 将分支master重置为要编辑的提交之前的提交
  3. 樱桃将temp分支中的所有提交一一挑选到master,并根据需要编辑消息
  4. 使用+推送更正后的提交行,以使用

    强制将新的提交行强制推送到现有分支
    git push origin +master
    

答案 3 :(得分:0)

好吧,我在搜索该页面时登陆了该页面。找到了许多其他选择的更好方法

git rebase -i HEAD~2

编辑器将打开并显示以下详细信息

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

git push -f 
相关问题