git 从git中的旧提交恢复文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6624036/
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
Restore file from old commit in git
提问by Varun Achar
I have an old commit that I did a few weeks ago. I want to restore only a single file from that commit. What do I do?
我有一个几周前做过的旧提交。我只想从该提交中恢复单个文件。我该怎么办?
回答by sehe
git checkout 'master@{7 days ago}' -- path/to/file.txt
This will not alter HEAD, it will just overwrite the local file path/to/file.txt
这不会改变 HEAD,它只会覆盖本地文件 path/to/file.txt
See man git-rev-parsefor possible revision specifications there (of course a simple hash (like dd9bacb
) will do nicely)
有关可能的修订规范,请参阅man git-rev-parse(当然,简单的哈希(如dd9bacb
)会很好)
Don't forget to commit the change (after a review...)
不要忘记提交更改(经过...)
回答by Urs Reupke
- Check out the file from your old commit via
git checkout [Revision_Key] -- path/to/file
. - Add, commit, push as appropriate.
- 通过
git checkout [Revision_Key] -- path/to/file
. - 根据需要添加、提交、推送。
回答by Salvador Valencia
I needed to restore a recent file committed into git. So just to reiterate and give another perspective, you need to do this by running the following two steps:
我需要恢复最近提交到 git 的文件。所以只是重申并给出另一个观点,您需要通过运行以下两个步骤来做到这一点:
git log -3
This shows the three most recent commits. Read the comments and the author's name so you narrow down what exact version you want. Write down that long commit id (i.e. b6b94f2c19c456336d60b9409fb1e373036d3d71) for the commit version you want.git checkout b6b94f2c19c456336d60b9409fb1e373036d3d71 -- myfile.java
Pass the commit id AND the file name you want to restore. Make sure you have a space before and after the double hyphen.
git log -3
这显示了最近的三个提交。阅读评论和作者姓名,以便缩小所需的确切版本。记下您想要的提交版本的长提交 ID(即 b6b94f2c19c456336d60b9409fb1e373036d3d71)。git checkout b6b94f2c19c456336d60b9409fb1e373036d3d71 -- myfile.java
传递提交 ID 和要恢复的文件名。确保双连字符前后都有空格。
There are many other ways to do it. But this one is the simpler one I can remember. Hope that helps.
还有很多其他方法可以做到。但这是我能记住的最简单的一个。希望有帮助。
NOTE:If you are inside your project path/folder then is not necessary to type the full file's path in the checkout command.
注意:如果您在项目路径/文件夹中,则无需在 checkout 命令中键入完整文件的路径。
回答by mjarosie
All answers mention git checkout <tree-ish> -- <pathspec>
. As of git v2.23.0 there's a new git restoremethod which is supposed to assume part of what git checkout
was responsible for. See highlights of changes on github blog.
所有答案都提到git checkout <tree-ish> -- <pathspec>
。从 git v2.23.0 开始,有一个新的git restore方法应该承担部分git checkout
责任。请参阅github 博客上的更改亮点。
The default behaviour of this command is to restore the state of a working tree with the content coming from the source
parameter (which in your case will be a commit hash).
此命令的默认行为是使用来自source
参数的内容(在您的情况下将是提交哈希)恢复工作树的状态。
Assuming the commit hash is abcdef
the command would look like this:
假设提交哈希是abcdef
命令,如下所示:
git restore --source=abcdef file_name
which (by default) puts it in working tree. If you want to put the change directly in index so it can be committed straight away:
它(默认情况下)将它放在工作树中。如果您想将更改直接放在索引中以便可以立即提交:
git restore --source=abcdef --worktree --staged file_name
or with short option names:
或使用简短的选项名称:
git restore -s=abcdef -W -S file_name