在Git存储库中查找和恢复已删除的文件 [英] Find and restore a deleted file in a Git repository

查看:156
本文介绍了在Git存储库中查找和恢复已删除的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我在一个Git仓库。我删除一个文件并提交更改。我继续工作,做更多的承诺。然后,我发现我需要恢复该文件。

我知道我可以检查一个文件使用 git checkout HEAD ^ foo.bar ,但我真的不知道该文件何时被删除。


  1. 什么是找到提交的最快方法删除一个给定的文件名?

  2. 将文件恢复到我的工作副本最简单的方法是什么?

我希望我不必手动浏览我的日志,检查整个项目的给定的SHA,然后手动将该文件复制到我原来的项目结帐。

解决方案

找到影响给定路径的最后一个提交。由于文件不在HEAD提交中,所以这个提交必须删除它。

  git rev-list -n 1 HEAD - < file_path> 

然后使用脱字符( ^ )符号:

  git checkout< deleting_commit> ^  - < file_path> 

或者在一个命令中,如果 $ file

  git checkout $(git rev-list -n 1 HEAD  - $ file)^ - $ file


Say I'm in a Git repository. I delete a file and commit that change. I continue working and make some more commits. Then, I find I need to restore that file.

I know I can checkout a file using git checkout HEAD^ foo.bar, but I don't really know when that file was deleted.

  1. What would be the quickest way to find the commit that deleted a given filename?
  2. What would be the easiest way to get that file back into my working copy?

I'm hoping I don't have to manually browse my logs, checkout the entire project for a given SHA and then manually copy that file into my original project checkout.

解决方案

Find the last commit that affected the given path. As the file isn't in the HEAD commit, this commit must have deleted it.

git rev-list -n 1 HEAD -- <file_path>

Then checkout the version at the commit before, using the caret (^) symbol:

git checkout <deleting_commit>^ -- <file_path>

Or in one command, if $file is the file in question.

git checkout $(git rev-list -n 1 HEAD -- "$file")^ -- "$file"

这篇关于在Git存储库中查找和恢复已删除的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆