修改一个不是以前提交的提交 [英] Amend a commit that wasn't the previous commit

查看:173
本文介绍了修改一个不是以前提交的提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常我会有一个如下所示的工作流:


  1. 将更改提交给一组文件

  2. 将更改提交到其他文件组

我不能使用 git commit --amend ,因为它不是最新的承诺我需要改变。

可以使用 git rebase 来解决这个问题。运行 git rebase -i sha1〜1 其中sha1是您想要更改的提交散列。找到您想要更改的提交,并用编辑替换选择,如rebase编辑器的评论中所述。当你从那里继续时,你可以编辑该提交。



请注意,这将改变该提交的sha1以及所有子项 - 换句话说,这个重写从这一点开始的历史。你可以打破这样做的仓库,但如果你没有推,这不是什么大问题。


Frequently I'll have a workflow like the following:

  1. Commit changes to a group of files
  2. Commit changes to a different group of files
  3. Realize I missed some changes that belong in the first commit
  4. Curse

I can't make use of git commit --amend because it's not the most recent commit that I need to change. What's the best way to add changes to the first commit without touching the second one?

解决方案

You can use git rebase to solve this. Run git rebase -i sha1~1 where sha1 is the commit hash of the one you want to change. Find the commit you want to change, and replace "pick" with "edit" as described in the comments of the rebase editor. When you continue from there, you can edit that commit.

Note that this will change the sha1 of that commit as well as all children -- in other words, this rewrites the history from that point forward. You can break repositories doing this, but if you haven't pushed, it's not as much of a big deal.

这篇关于修改一个不是以前提交的提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆