简体   繁体   English

修改倒数第二个提交

[英]Amend the second to last commit

If you found a bug in your last commit, you can simply recommit the last commit with如果您在上次提交中发现了错误,您可以简单地重新提交上次提交

git commit --amend

But if you already committed another commit, how do you resubmit the commit before that one?但是如果你已经提交了另一个提交,你如何在那个提交之前重新提交?


Note: I assume, you are aware, that you should only use --amend if you are sure no other developer already used your commit upstream注意:我假设您知道,如果您确定没有其他开发人员已经在上游使用您的提交,您应该只使用--amend

  1. commit your fixup with提交你的修正

    git commit -a -m "fix commit (this one will be shifted up one line)" git commit -a -m "fix commit (这个会往上移一行)"

    (the commit message is not important, it will be obsolete once you are finished) (提交信息并不重要,一旦你完成它就会过时)

  2. Now the magic:现在魔术:
    rebase the HEAD to the second last commit but edit the commit message before in that way, that you swap the last and the last but one line in the message editor:将 HEAD 重新设置为倒数第二次提交,但之前以这种方式编辑提交消息,即在消息编辑器中交换最后一行和最后一行

     git rebase -i HEAD~3 git rebase -i HEAD~3

    The editor will show the last 3 comits like this:编辑器将像这样显示最后 3 个命令:

     pick 2a06f16 this was the last commit选择 2a06f16 这是最后一次提交\n pick 0dbc5ce the last-but-one commit选择 0dbc5ce 最后一个提交\n pick 2e30418 fix commit (this one will be shifted up one line)选择 2e30418 修复提交(此将向上移动一行)\n\n # Rebase 011f3d0..2e30418 onto 011f3d0 # 将 011f3d0..2e30418 变基到 011f3d0\n # … # ...\n

    swap the last two lines so the commit message will look for example like this:交换最后两行,以便提交消息看起来像这样:

     pick 2a06f16 this was the last commit选择 2a06f16 这是最后一次提交\n fixup 2e30418 fix commit (this one will be shifted up one line) fixup 2e30418 fix commit(这个会上移一行)\n pick 0dbc5ce the last-but-one commit选择 0dbc5ce 最后一个提交\n\n # Rebase 011f3d0..2e30418 onto 011f3d0 # 将 011f3d0..2e30418 变基到 011f3d0\n # … # ...\n
  3. Check your log with检查您的日志

    git log HEAD~3 git log HEAD~3
  4. push the corrected commit-line with + to force a new push to the already existing branch with+推送更正的提交行以强制新的推送到已经存在的分支

    git push origin +branchname git push origin +branchname

@aragaer 's comment is even shorter way of doing the same: @aragaer 的评论是做同样事情的更短的方式:

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

then然后

git rebase -i HEAD~3

and you don't need to change anything, so you can just close editor and git will handle the fixup itself.并且您不需要更改任何内容,因此您只需关闭编辑器, git将自行处理修复。

Note: This doesn't work on my Ubuntu system!注意:这不适用于我的 Ubuntu 系统!

Well, I landed on this page while searching for the same.好吧,我在搜索相同页面时登陆了此页面。 Found a better way with many other options通过许多其他选项找到了更好的方法

git rebase -i HEAD~2

An editor will open up with the following details编辑器将打开,其中包含以下详细信息

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>]

press i, change pick to r or reword , something like below按 i,将pick更改为rreword ,如下所示

pick 4f4f96f Added git ignore
r d01e18c Added sample Blog

PRESS esc + : + wqesc + : + wq

Another window will open up, change the commit message将打开另一个窗口,更改提交消息

PRESS esc + : + wqesc + : + wq

git push -f 

In a git GUI you can easily achieve this:在 git GUI 中,您可以轻松实现这一点:

  1. add a temp branch to the newest committemp分支添加到最新的提交
  2. reset the branch master to the commit before the commit you want to edit在您要编辑的提交之前将分支master重置为提交
  3. cherry pick all commits from the temp branch one by one to master and edit the messages if desired樱桃从temp分支中一一选择所有提交以master和编辑消息(如果需要)
  4. push the corrected commit-line with + to force a new push to the already existing branch with+推送更正的提交行以强制新的推送到已经存在的分支

    git push origin +master

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM