签出单个已删除文件时,如何解决 pathspec 与 git 已知的任何文件不匹配的问题?

How to resolve pathspec not matching any file known to git when checking out a single, deleted file?

我有一个分支 feature 已经脱离 develop 并且在它前面。
stash popmerge develop 的组合导致提交包含特定文件的 rm

我正在尝试通过在删除之前从提交中检出文件来撤消此删除:

name@machine ~/path/inside/the/repo/to/the/parent/dir
$ git checkout megalongshaofthecommitthatdeletedthefile^ -- filename.ext

但得到

error: pathspec 'filename.ext' did not match any file(s) know to git.

我试过签出未删除的文件,但git仍然拒绝签出未删除的文件。

我如何告诉 git 从哪里获得这个文件?

How can I tell git where to get this file?

可以做什么?

  1. 使用 git reflog 并在需要时在更改之前检查之前的提交。

    reflog is the full history of your repository which allow you to go back to any desired point in time

  2. git log --follow <file> 将显示修改文件的提交(最后更改将是第一个)

    This command will display all the log entries for the given file so you can grab the required commit and take the file form there using git checkout SHA-1 file_path

  3. git bisect 搜索删除文件的提交。

    Bisect is the way to search for "bugs" and or changes made to the repository use it to track when the file was deleted. you can write script that search if the file exists and run it with the git bisect --run... flag

  4. 获得所需的 SHA-1 后,使用 git revert SHA-1 到 "undo" 在给定提交中所做的更改。为了安全起见,我假设你想在一个新的分支中这样做。

  5. 如果您只需要文件而不需要其他更改:
    git checkout SHA-1 -- file_path

pathspec 问题的解决方案是指定父目录,相对于 bash 正在查看的目录,而不仅仅是文件名,因为 git 不一定是 运行宁在 bash 是 "on".

的同一目录中

假设 git 运行 宁在 C:/Program Files (x86)/Git/

name@machine ~/path/inside/the/repo/to/the/parent/dir
$ git checkout megalongshaofthecommitthatdeletedthefile^ -- filename.ext

将寻找 C:/Program Files (x86)/Git/filename.ext.

相反,运行:

name@machine ~/path/inside/the/repo/to/the/parent/dir
$ git checkout megalongshaofthecommitthatdeletedthefile^ -- ../dir/filename.ext

这将寻找 C:/repodir/path/inside/the/repo/to/the/parent/dir/filename.ext