简体   繁体   English

Git忘记将文件更改添加到尚未推送的提交中(不是最后一个)

[英]Git forgot to add file change to a not yet pushed commit (not the last one)

I want to add a file change to a not yet pushed commit (not the last one). 我想将文件更改添加到尚未推送的提交(而不是最后一个)。

Let's say I have the following scenario: 假设我有以下情形:

git init
touch test.txt
// do some changes to the text file
git commit -a -m "First change"

# if I now change something I can add it to the previous commit with:
git add test.txt
git commit --amend -C HEAD


# more changes to test.txt
git commit -a -m "Second change"

# more changes to test.txt
git commit -a -m "Third change"

# one more change
# how can I add the change to my second commit (second change)?

I hope the question is clear. 我希望问题清楚。 If not, let me know. 如果没有,请告诉我。 I tried git commit --amend -C HEAD~2 , but that one caused some weird trouble in my real git project. 我尝试了git commit --amend -C HEAD~2 ,但是那在我的实际git项目中造成了一些奇怪的麻烦。

You could use git rebase . 您可以使用git rebase I would recommend reading up on git help rebase and especially the INTERACTIVE_MODE section as it is wise to be careful while performing the rebase. 我建议阅读git help rebase ,尤其是INTERACTIVE_MODE部分,因为执行rebase时要小心谨慎。

Assuming your last commit was "Third change" you could: 假设您的最后一次提交是“第三次更改”,则可以:

  1. Invoke git rebase -i HEAD~2 调用git rebase -i HEAD~2
  2. git will invoke your editor with two lines showing the second and third commits, with a "pick" prefix on each. git将用两行来显示第二和第三次提交来调用您的编辑器,每行上都有一个“ pick”前缀。 Change the "pick" prefix for your "Second change" commit to "edit". 将“第二次更改”提交的“ pick”前缀更改为“ edit”。 It should then look like: 然后应如下所示:
 edit xxxxxxx Second change pick xxxxxxx Third change 
  1. Save and exit your editor. 保存并退出编辑器。
  2. You will now be rolled back to a state "right after the second change" and git will stop to allow you to edit/amend the commit. 现在,您将被退回到“第二次更改之后”的状态,并且git将停止以允许您编辑/修改提交。 So you can now git add other files and once you're ready do a git commit --amend which will load up an editor where you can edit the second change's commit log message. 因此,您现在可以git add其他文件,一旦准备好就执行git commit --amend ,它将加载一个编辑器,您可以在其中编辑第二个更改的提交日志消息。
  3. Once you're happy with the new "Second change", issue a git rebase --continue . 一旦对新的“第二次更改”感到满意,请发出git rebase --continue This will replay the third change over your revised second change. 这将重播您修改后的第二个更改中的第三个更改。

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

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