我可以在 GIT 中进行部分还原吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5669358/
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
Can I do a partial revert in GIT
提问by Clutch
Is it possible to revert only a single file or certain changes in a file in multi file commit?
是否可以在多文件提交中仅还原单个文件或文件中的某些更改?
Full storyI committed a bunch of files. A number of commits later someone who will remain nameless (Hyman!!!) copied a file into his repository and committed several files, overwriting some of the changes I did. I want to revert the one file that got clobbered or better yet, go in and revert two changes in that file. This will have to be a separate revert commit since it was pulled and pushed.
完整的故事我提交了一堆文件。一些提交之后,将保持匿名的人(Hyman !!!)将一个文件复制到他的存储库中并提交了几个文件,覆盖了我所做的一些更改。我想恢复被破坏的一个文件或更好的文件,进入并恢复该文件中的两个更改。这将必须是一个单独的恢复提交,因为它被拉和推。
回答by bobDevil
You can revert the commit without creating a new one by adding the '--no-commit' option. This leaves all the reverted files in the staging area. From there, I'd perform a soft reset and add in the changes I really wanted. For an example workflow:
您可以通过添加“--no-commit”选项来恢复提交而无需创建新的提交。这会将所有恢复的文件留在暂存区中。从那里,我将执行软重置并添加我真正想要的更改。对于示例工作流程:
git revert <sha-of-bad-commit> --no-commit
git reset // This gets them out of the staging area
<edit bad file to look like it should, if necessary>
git add <bad-file>
git checkout . // This wipes all the undesired reverts still hanging around in the working copy
git commit
回答by cmcginty
You can interactively apply old version of a file using the checkout
command.
您可以使用该checkout
命令以交互方式应用旧版本的文件。
For example, if you know the COMMIT
where the code to add back was removed, you can run the following command:
例如,如果您知道COMMIT
要添加回的代码被删除的位置,则可以运行以下命令:
git checkout -p COMMIT^ -- FILE_TO_REVERT
Git will prompt you to add back the hunks that are missing from the current version of the file. You can use e
to create a patch of the change before applying it back.
Git 会提示你重新添加当前版本文件中缺少的块。您可以e
在将更改应用回之前创建更改的补丁。
回答by Brian Campbell
You can just manually check out the old, good contents of the files you want to revert using git checkout
. For instance, if you want to revert my-important-file
to the version it was in the version abc123
, you can do
您只需手动检出要使用git checkout
. 例如,如果你想恢复my-important-file
到它在 version 中的版本abc123
,你可以这样做
git checkout abc123 -- my-important-file
Now you have the old contents of my-important-file
back, and can even edit them if you feel like, and commit as usual to make a commit which will revert the changes that he made. If there are only some parts of his commit that you want to revert, use git add -p
to select only a few hunks from the patch that you are committing.
现在你有了my-important-file
back的旧内容,如果你愿意,甚至可以编辑它们,并像往常一样提交来进行提交,这将恢复他所做的更改。如果您只想还原他提交的某些部分,请使用git add -p
仅从您提交的补丁中选择几个大块头。
回答by ntc2
I found a way to do this on the Git mailing list:
我在 Git 邮件列表中找到了一种方法:
git show <commit> -- <path> | git apply --reverse
Source: http://git.661346.n2.nabble.com/Revert-a-single-commit-in-a-single-file-td6064050.html#a6064406
来源:http: //git.661346.n2.nabble.com/Revert-a-single-commit-in-a-single-file-td6064050.html#a6064406
Variations
变化
That command fails (causing no changes) if the patch does not apply cleanly, but with --3way
you instead get conflicts which you can then resolve manually (in this case Casey's answermight be more practical):
如果补丁没有完全应用,该命令将失败(不会导致任何更改),但--3way
您会遇到冲突,然后您可以手动解决这些冲突(在这种情况下,凯西的回答可能更实用):
git show <commit> -- <path> | git apply --reverse --3way
You can also use this to partially revert multiple commits, e.g.:
您还可以使用它来部分还原多个提交,例如:
git log -S<string> --patch | git apply --reverse
to revert files with changes matching <string>
in any commit. This is exactly what I needed in my use case (a few separate commits introduced similar changes to different files, along with changes other files in unrelated ways that I did not want to revert).
恢复<string>
在任何提交中具有匹配更改的文件。这正是我在用例中所需要的(一些单独的提交引入了对不同文件的类似更改,以及以我不想恢复的不相关方式更改其他文件)。
If you have diff.noprefix=true
set in your ~/.gitconfig
then you need to add -p0
to the git apply
command, e.g.
如果你已经diff.noprefix=true
设置了~/.gitconfig
那么你需要添加-p0
到git apply
命令中,例如
git show <commit> -- <path> | git apply -p0 --reverse