如何将更改的文件添加到 Git 中较旧的(不是最后一次)提交
在过去的一个小时里,我改变了几件事,并一步步提交它们,但我刚刚意识到我忘记在一些提交之前添加更改的文件(例如,标记为 a0865...
如下)。
日志看起来像这样:
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
有什么想法吗?
I have changed several things over the last hour and committed them step by step, but I just realized I've forgot to add a changed file some commits ago (for example, the commit marked as a0865...
below).
The Log looks like this:
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
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
使用
git rebase
。具体来说:pick
更改为edit
,将有问题的提交 (a0865...
) 标记为编辑。不要删除其他行,因为这会删除提交。[^vimnote]git add
添加文件。--force
才能在远程更新它们。但是,有关使用--force
的常见警告适用,如果您不小心并事先与他们协调,您很容易会丢失其他人的工作。[^vimnote]:如果您使用
vim
,则必须按 Insert 键进行编辑,然后按 Esc 并输入:wq
保存文件、退出编辑器并应用更改。或者,您可以配置用户友好的 git commit 编辑器,带有git config --global core.editor "nano"
。Use
git rebase
. Specifically:git stash
to store the changes you want to add.git rebase -i HEAD~10
(or however many commits back you want to see).a0865...
) for edit by changing the wordpick
at the start of the line intoedit
. Don't delete the other lines as that would delete the commits.[^vimnote]git stash pop
.git add <file>
.git commit --amend --no-edit
.git rebase --continue
which will rewrite the rest of your commits against the new one.--force
again to update them on the remote. However, the usual warnings about using--force
apply, and you can easily lose other people's work if you are not careful and coordinate with them beforehand.[^vimnote]: If you are using
vim
then you will have to hit the Insert key to edit, then Esc and type in:wq
to save the file, quit the editor, and apply the changes. Alternatively, you can configure a user-friendly git commit editor withgit config --global core.editor "nano"
.要通过小的更改“修复”旧提交,而不更改旧提交的提交消息,其中
OLDCOMMIT
类似于091b73a
:您还可以使用
git commit --squash=OLDCOMMIT
在变基期间编辑旧的提交消息。请参阅 git commit 和 git rebase。一如既往,当重写 git 历史记录时,你应该只修复或压缩提交您尚未发布给其他任何人(包括随机互联网用户和构建服务器)。
详细说明
git commit --fixup=OLDCOMMIT
复制OLDCOMMIT
提交消息并自动添加前缀fixup!
,以便在执行期间可以将其按正确的顺序放置交互式变基。 (--squash=OLDCOMMIT
执行相同操作,但添加前缀
squash!
。)git rebase --interactive
将打开一个文本编辑器(可以配置)以确认(或编辑)rebase 指令序列 。文件中有变基指令更改的信息;只需保存并退出编辑器(:wq
invim
)继续变基。--autosquash
< /a> 将自动以正确的顺序放置任何--fixup=OLDCOMMIT
提交。请注意,--autosquash
仅在使用--interactive
选项时才有效。OLDCOMMIT^
^
code> 表示它是对OLDCOMMIT
之前提交的引用。 (OLDCOMMIT^
是OLDCOMMIT
的第一个父级。)可选自动化
上述步骤非常适合验证和/或修改变基指令序列,但也可以通过以下方式跳过/自动化交互式变基文本编辑器:
GIT_SEQUENCE_EDITOR
设置为一个脚本。To "fix" an old commit with a small change, without changing the commit message of the old commit, where
OLDCOMMIT
is something like091b73a
:You can also use
git commit --squash=OLDCOMMIT
to edit the old commit message during rebase.See documentation for git commit and git rebase. As always, when rewriting git history, you should only fixup or squash commits you have not yet published to anyone else (including random internet users and build servers).
Detailed explanation
git commit --fixup=OLDCOMMIT
copies theOLDCOMMIT
commit message and automatically prefixesfixup!
so it can be put in the correct order during interactive rebase. (--squash=OLDCOMMIT
does the same but prefixessquash!
.)git rebase --interactive
will bring up a text editor (which can be configured) to confirm (or edit) the rebase instruction sequence. There is info for rebase instruction changes in the file; just save and quit the editor (:wq
invim
) to continue with the rebase.--autosquash
will automatically put any--fixup=OLDCOMMIT
commits in the correct order. Note that--autosquash
is only valid when the--interactive
option is used.^
inOLDCOMMIT^
means it's a reference to the commit just beforeOLDCOMMIT
. (OLDCOMMIT^
is the first parent ofOLDCOMMIT
.)Optional automation
The above steps are good for verification and/or modifying the rebase instruction sequence, but it's also possible to skip/automate the interactive rebase text editor by:
GIT_SEQUENCE_EDITOR
to a script.在 git 1.7 中,有一种非常简单的方法来使用 git rebase:
暂存文件:
创建一个新的提交并重新使用“损坏的”提交前缀的提交消息
fixup!
在主题行中(或squash!
如果您想编辑提交(消息)):使用
git rebase -i --autosquash
来修复您的提交with git 1.7, there's a really easy way using
git rebase
:stage your files:
create a new commit and re-use commit message of your "broken" commit
prepend
fixup!
in the subject line (orsquash!
if you want to edit commit (message)):use
git rebase -i --autosquash
to fixup your commit您可以尝试
rebase --interactive
< /strong> 会话来修改您的旧提交(前提是 您尚未将这些提交推送到另一个存储库)。You can try a
rebase --interactive
session to amend your old commit (provided you did not already push those commits to another repo).这是一个实现 @Greg 答案的函数:
用法:(仅在暂存中进行预期的更改)
gitamendoldcommit $OLDCOMMIT
Here's a function that implements @Greg's answer:
Usage: (while having only the intended changes in the staging)
gitamendoldcommit $OLDCOMMIT
此外,如果您使用 git rebase -i 并想要转到当前分支的第一次提交,则可以使用 git rebase -i --root 。现在您可以轻松修改您的第一次提交。
Aditionally, if you use
git rebase -i
and want to go to the first commit of your current branch you can usegit rebase -i --root
. Now you could easily modify your first commit.2023 年,有一些工具可以做到这一点:特别是 gitsorb。它是一个很棒的工具,有可能改变许多尘埃落定的 git 工作流程。
In 2023 there are tools that do this: specifically git absorb. It is a great tool with the potential to change a lot of dusty git workflows.