我在过去一个小时内更改了几项内容并逐步提交,但我才意识到我忘记添加一些提交前更改的文件。
日志看起来像这样:
GIT TidyUpRequests u:1 d:0> git log
commit fc6734b6351f6c36a587dba6dbd9d5efa30c09ce
Author: David Klein <>
Date: Tue Apr 27 09:43:55 2010 +0200
The Main program now tests both Webservices at once
commit 8a2c6014c2b035e37aebd310a6393a1ecb39f463
Author: David Klein <>
Date: Tue Apr 27 09:43:27 2010 +0200
ISBNDBQueryHandler now uses the XPath functions from XPath.fs too
commit 06a504e277fd98d97eed4dad22dfa5933d81451f
Author: David Klein <>
Date: Tue Apr 27 09:30:34 2010 +0200
AmazonQueryHandler now uses the XPath Helper functions defined in XPath.fs
commit a0865e28be35a3011d0b6091819ec32922dd2dd8 <--- changed file should go here
Author: David Klein <>
Date: Tue Apr 27 09:29:53 2010 +0200
Factored out some common XPath Operations
有什么想法吗?
最佳答案
使用 git rebase
.具体来说:
- 使用
git stash
存储您要添加的更改。 - 使用
git rebase -i HEAD~10
(或者无论您希望看到多少次提交)。 - 通过更改单词
a0865...
来标记有问题的提交 (pick
) 以进行编辑在行的开头进入edit
.不要删除其他行,因为那样会删除提交。[^vimnote] - 保存 rebase 文件,git 将返回到 shell 并等待您修复该提交。
- 使用
git stash pop
pop stash - 使用
git add <file>
添加您的文件. - 使用
git commit --amend --no-edit
修改提交. - 做一个
git rebase --continue
这将根据新提交重写您的其余提交。 - 如果您标记了多个提交进行编辑,请从第 2 步开始重复。
[^vimnote]: 如果你使用 vim
然后你必须按 Insert 键进行编辑,然后按 Esc 并输入 :wq
保存文件,退出编辑器,并应用更改。或者,您可以 configure a user-friendly git commit editor与 git config --global core.editor "nano"
.
关于git - 如何将更改的文件添加到 Git 中较旧的(不是最后的)提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2719579/