git 如何更改过去的提交以包含丢失的文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14369155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change past commit to include a missed file?
提问by kolrie
I have committed a change and forgot to add a file to the change set. After other commits, I realized the file is now missing from a HEAD^4
commit.
我已提交更改但忘记将文件添加到更改集。在其他提交之后,我意识到该文件现在从HEAD^4
提交中丢失了。
How do I rewrite a previous commit to include the missing file?
如何重写以前的提交以包含丢失的文件?
采纳答案by Rafa? Rawicki
Use git rebase --interactive HEAD~4
and set edit
option for the commit you'd like to amend.
为您要修改的提交使用git rebase --interactive HEAD~4
和设置edit
选项。
Remember that you should notmodify commits pushed to the remote repository this way. It's better to add a new commit with missing file in that case.
请记住,您不应以这种方式修改推送到远程存储库的提交。在这种情况下,最好添加一个缺少文件的新提交。
回答by Dr Beco
I realize people can google and come here to find a simpler answer: What if it was just the last commit?(OP's question is for fixing the 4th commit back in history)
我意识到人们可以通过谷歌搜索并来到这里找到一个更简单的答案:如果这只是最后一次提交怎么办?(OP 的问题是修复历史上的第 4 次提交)
In the case you commit and realize you forgot to add some file immediately, just do:
如果您提交并意识到您忘记立即添加一些文件,请执行以下操作:
# edited file-that-i-remember.txt
git add file-that-i-remember.txt
git commit
# realize you forgot a file
git add file-that-i-forgot.txt
git commit --amend --no-edit
Where --no-edit
will keep the same commit message.
哪里--no-edit
会保留相同的提交信息。
Easy peasy!
十分简单!
回答by mvp
If you have NOT pushed these 4 commits, you can do it as follows:
如果您还没有推送这 4 次提交,您可以按如下方式进行:
Create patch files for all these commits:
为所有这些提交创建补丁文件:
git format-patch -4
Rewind back by 4 commits:
回退 4 次提交:
git reset --hard HEAD~4
Add missing file:
添加丢失的文件:
git add missing-file
Commit it with --amend
:
提交它--amend
:
git commit --amend
Apply all saved patches back:
应用所有保存的补丁:
git am *.patch
If you have pushed, you should NOT use this method. Instead, just admit your blunder and create one more commit on top of HEAD which fixes this issue.
如果您已推送,则不应使用此方法。相反,只需承认您的错误并在 HEAD 之上再创建一次提交即可解决此问题。
回答by dominik
Although the accepted answer is correct, it lacks detailed instructions on how to perform editing a commit during a rebase process.
尽管接受的答案是正确的,但它缺乏有关如何在变基过程中执行编辑提交的详细说明。
First, start a rebase process:
git rebase --interactive HEAD~4
A list of commits will be presented, choose a commit you want to edit by changing the word
pick
toedit
and save the file.Make necessary modifications in your code (remember to invoke
git add
for new files)After all modification are done, issue
git commit --amend
- this will amend a commit marked asedit
Invoke
git rebase --continue
that will finish the process (if there are more commits marked asedit
, the above steps need to be repeated)
首先,启动一个 rebase 过程:
git rebase --interactive HEAD~4
将显示提交列表,通过将单词更改
pick
为edit
并保存文件来选择要编辑的提交。在您的代码中进行必要的修改(记住调用
git add
新文件)完成所有修改后,发出
git commit --amend
- 这将修改标记为的提交edit
git rebase --continue
将完成该过程的调用(如果标记为 的提交较多edit
,则需要重复上述步骤)
Important notes:
重要笔记:
DO NOT remove lines marked as
pick
that you don't want to edit - leave them as is. Deleting these lines will result in deleting related commitsGIT forces you to
stash
before rebasing if your working directory is not clean; you can howevergit stash pop / git stash apply
during rebase, in order to amend these changes (i.e. changes stashed before starting the rebase process) to a commit marked asedit
if something went wrong and you want to revert changes made during the rebase process before it finished (i.e. you want to revert to the point before starting the rebase), use
git rebase --abort
- also read: How to abort an interactive rebase if --abort doesn't work?As said in the accepted answer:
Remember that you should not modify commits pushed to the remote repository this way. It's better to add a new commit with missing file in that case.
The answer why, is in the Git Book(paragraph entitled "The Perils of Rebasing"):
Do not rebase commits that exist outside your repository.
If you follow that guideline, you'll be fine. If you don't, people will hate you, and you'll be scorned by friends and family.
When you rebase stuff, you're abandoning existing commits and creating new ones that are similar but different. If you push commits somewhere and others pull them down and base work on them, and then you rewrite those commits with git rebase and push them up again, your collaborators will have to re-merge their work and things will get messy when you try to pull their work back into yours.
[...]
不要删除标记为
pick
您不想编辑的行 - 保持原样。删除这些行将导致删除相关的提交stash
如果您的工作目录不干净,则GIT 会强制您在变基之前这样做;但是git stash pop / git stash apply
,您可以在变基期间,将这些更改(即在开始变基过程之前隐藏的更改)修改为标记为的提交edit
如果出现问题并且您想在 rebase 过程完成之前还原在 rebase 过程中所做的更改(即您想恢复到开始 rebase 之前的点),请使用
git rebase --abort
- 另请阅读:How to abort an interactive rebase if --abort does'工作吗?正如接受的答案中所说:
请记住,您不应以这种方式修改推送到远程存储库的提交。在这种情况下,最好添加一个缺少文件的新提交。
原因的答案在Git Book 中(标题为“ The Perils of Rebasing”的段落):
不要对存在于您的存储库之外的提交进行变基。
如果你遵循这个指导方针,你会没事的。如果你不这样做,人们会恨你,你会被朋友和家人鄙视。
当您重新设置内容时,您将放弃现有提交并创建相似但不同的新提交。如果你将提交推到某个地方,而其他人将它们拉下来并在它们的基础上工作,然后你用 git rebase 重写这些提交并再次将它们向上推,你的合作者将不得不重新合并他们的工作,当你尝试这样做时,事情会变得混乱把他们的工作拉回你的工作。
[...]