简体   繁体   English

如何“提取”我提交的文件?

[英]How do I “extract” my commited files?

I have two files file1.c and file1.h in my working directory /tmp/working . 我的工作目录/tmp/working有两个文件file1.cfile1.h Everything I do is on my local file system. 我所做的一切都在我的本地文件系统上。

I do a git init in /tmp/working that creates a .git directory in it. 我在/tmp/working中执行git init在其中创建一个.git目录。 Then I git add file1.* and git commit -m "Feb 13th 2017" . 然后我git add file1.*git commit -m "Feb 13th 2017"

On Feb 17 I accidentally delete the two files in my working directory. 2月17日,我不小心删除了工作目录中的两个文件。 How do I restore my files in my working directory from my local git repository? 如何从本地git存储库恢复工作目录中的文件? I don't want to undo the last commit or something like that, just want the copy of my files (version of Feb 13th) back in my working directory. 我不想撤消上一次提交或类似的操作,只想将我的文件副本(2月13日的版本)放回到我的工作目录中。

You can try just checking out those two files from the latest commit: 您可以尝试从最新提交中检出这两个文件:

git checkout -- path/to/file1.c
git checkout -- path/to/file1.h

The nice thing about Git is that it is hard to really mess things up. 关于Git的好处是,很难真正弄乱事情。 You only deleted those two files locally in the current commit. 您仅在当前提交中本地删除了这两个文件。 But their history is easily accessible using git checkout . 但是可以使用git checkout轻松访问他们的历史记录。


Actually, any path works: 实际上,任何路径都可以:

git checkout -- path/to/    # extracts the whole "path/to" directory
git checkout -- .           # extract all the content of the last commit

and you can also specify any commit : 您还可以指定任何提交:

git checkout other_branch -- path/to/   # extracts content from other_branch
git checkout v1.7.3 -- path/to/         #                  from this tag
git checkout eacf33b -- path/to/        #                  from this commit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM