git - 如何将更改的文件添加到 Git 中较旧的(不是最后的)提交

标签 git

我在过去一个小时内更改了几项内容并逐步提交,但我才意识到我忘记添加一些提交前更改的文件。

日志看起来像这样:

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 .具体来说:

  1. 使用git stash存储您要添加的更改。
  2. 使用git rebase -i HEAD~10 (或者无论您希望看到多少次提交)。
  3. 通过更改单词 a0865... 来标记有问题的提交 ( pick ) 以进行编辑在行的开头进入 edit .不要删除其他行,因为那样会删除提交。[^vimnote]
  4. 保存 rebase 文件,git 将返回到 shell 并等待您修复该提交。
  5. 使用git stash pop pop stash
  6. 使用 git add <file> 添加您的文件.
  7. 使用 git commit --amend --no-edit 修改提交.
  8. 做一个git rebase --continue这将根据新提交重写您的其余提交。
  9. 如果您标记了多个提交进行编辑,请从第 2 步开始重复。

[^vimnote]: 如果你使用 vim然后你必须按 Insert 键进行编辑,然后按 Esc 并输入 :wq保存文件,退出编辑器,并应用更改。或者,您可以 configure a user-friendly git commit editorgit config --global core.editor "nano" .

关于git - 如何将更改的文件添加到 Git 中较旧的(不是最后的)提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2719579/

相关文章:

git - 恢复 git 存储库/提交等

git - 如何 pull 入非当前分支?

linux - 如何将 Git 安装到没有 apt-get 的服务器上?

git - 如何让.gitignore忽略文件?

git - 如何用上游版本覆盖未 merge 的 git checkout

git - 使用 gitattributes 和 gitconfig 替换文件中的 aws 键时丢失单引号

git - 如何在 emacs 中禁用 vc-git?

git - jenkins 构建完成后如何将更改推送到 github?

git - 如何在不通过我的本地计算机抽取所有内容的情况下将 Github 上的一个分支更新到上游版本?

git - 命令行中的Msbuild错误,Error MSB4067