[英]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: 假设您的最后一次提交是“第三次更改”,则可以:
git rebase -i HEAD~2
调用git rebase -i HEAD~2
edit xxxxxxx Second change pick xxxxxxx Third change
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
,它将加载一个编辑器,您可以在其中编辑第二个更改的提交日志消息。 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.