git 修改不是之前提交的提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3926768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 09:17:02  来源:igfitidea点击:

Amend a commit that wasn't the previous commit

git

提问by Robert Speicher

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
  1. 提交对一组文件的更改
  2. 提交对不同文件组的更改
  3. 意识到我错过了一些属于第一次提交的更改
  4. 诅咒

I can't make use of git commit --amendbecause 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?

我无法使用,git commit --amend因为它不是我需要更改的最新提交。在不触及第二个提交的情况下向第一个提交添加更改的最佳方法是什么?

回答by Daenyth

You can use git rebaseto solve this. Run git rebase -i sha1~1where 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.

你可以用它git rebase来解决这个问题。运行git rebase -i sha1~1其中 sha1 是您要更改的提交哈希。找到您要更改的提交,并将“pick”替换为“edit”,如 rebase 编辑器的注释中所述。当您从那里继续时,您可以编辑该提交。

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.

请注意,这将更改该提交以及所有子项的 sha1 - 换句话说,这将重写从那时起的历史记录。您可以这样做来破坏存储库,但如果您还没有推送,那也没什么大不了的。