1

I'm in the process of preparing a pull request and the one of the points was to keep some legacy files around that removed. This happened a couple commits back from the current HEAD. Following the process to cherry-pick a single file from a commit:

git checkout 1234567 -- src/Reporters/DeletedFile.h src/Reporters/DeletedFile.cpp

I get the following error message from Git Bash:

error: pathspec 'src/Reporters/DeletedFile.h' did not match any file(s) known to git
error: pathspec 'src/Reporters/DeletedFile.cpp' did not match any file(s) known to git

Checking with show indicates that the files were deleted in hash 1234567 and using the full hash also results in the same error. What could be causing this error?

rjzii
  • 14,236
  • 12
  • 79
  • 119

2 Answers2

0

Hash 1234567 does not contain the file that you want to checkout, as it has just been deleted. You will need to checkout the file from the commit before the delete.

To get the commit before, you can use <hash>^1 or just <hash>^ as per this answer.

Dezza
  • 1,094
  • 4
  • 22
  • 25
0

The command you need to run is

git checkout 1234567^ -- src/Reporters/DeletedFile.h src/Reporters/DeletedFile.cpp

This will pick the commit right before 1234567, which is the last commit that contains the file you need.

ArneJ
  • 1
  • 1
  • I just tried the same here with a small test repository and it works fine. Are you sure that the paths of the two files are correct? – ArneJ Aug 15 '19 at 15:16
  • @ArneJ: Be sure that when you do this `git checkout`, you're at the top level of the work-tree associated with the repository. Alternatively, use the pathspec syntax that specifies "top of repository" (`:/:src/Reporters/...` or `:(top)src/Reporters/...`). – torek Aug 15 '19 at 15:21
  • @ArneJ Yes, double checked, copy pasted, etc. However, I'm wondering if the fact the hash is from this is seven commits (one of which was a major merge) back that git is being tripped up by something. – rjzii Aug 15 '19 at 15:24
  • @torek Getting a syntax error when trying to do `:(top)src/...` – rjzii Aug 15 '19 at 15:25
  • To see which commits changed a file, you can try `git log --stat -- path/to/deleted/file`. This will show all commits that changed the path. Maybe you find another commit which deleted the file and can then use that commit hash with the proposed checkout command. – ArneJ Aug 15 '19 at 15:32
  • @rjzii: parentheses are special to sh/bash, so depending on your command line environment you might need to quote them. The `:/:` notation suffers less from that particular problem. – torek Aug 15 '19 at 17:41
  • @rjzii: hm, might help if you showed the errors from these various options. Note that you can also use `git show :` to view the contents of the given path in the given hash. The `` part here is *not* a pathspec so it does not suffer from the "relative path based on $pwd vs repo-top-level" issue. – torek Aug 15 '19 at 19:25
  • @torek Literally the same errors as in the question, `git checkout e116ae8^ -- :/:src/Reporters/DeletedFile.h :/:src/Reporters/DeletedFile.cpp` – rjzii Aug 16 '19 at 13:43
  • The only remaining options I can think of at this point include things like Windows case-folding issues or Windows command-line `^` issues. (I don't use Windows so I'm not entirely sure what the `^` issue is.) If you're not on Windows, I'm out of additional suggestions for the `checkout`, but you could inspect commit `e116ae8^` directly (use `git ls-tree -r` on it for instance). – torek Aug 16 '19 at 15:39