toofarsideways
toofarsideways

Reputation: 3972

Git log says file is there but cannot checkout

Is it possible to call git log filepath and see that a file is in a commit, but be unable to check it out using git checkout filepath?

error: pathspec 'filepath' did not match any file(s) known to git.

I'm in the same working tree.

EDIT: Added example containing git log filepath, git diff sha1id and git checkout filepath.

> git log 889e2e74-6ec3-448b-ad35-feb2187d9d2b

commit b2df50ecd14472a1c38d2cd2269898d4f3ab9604
Author: l
Date:   Tue Apr 16 15:20:39 2013 +0100

    Committed 889e2e74-6ec3-448b-ad35-feb2187d9d2b

commit dc9ee9433fd87c9031e7569ab451d2c02343e146
Author: l
Date:   Tue Apr 16 15:20:38 2013 +0100

    Committed 3f743855-ec1b-4e76-808a-eafc6042c47a

commit 79eed05ab387f0f6aec266df109d7cda26789c83
Author: f
Date:   Tue Apr 16 15:19:58 2013 +0100

    Committed 889e2e74-6ec3-448b-ad35-feb2187d9d2b

git diff dc9ee9
diff --git a/3f743855-ec1b-4e76-808a-eafc6042c47a b/3f743855-ec1b-4e76-808a-eafc6042c47a
index 612e91c..619ac4f 100644
Binary files a/3f743855-ec1b-4e76-808a-eafc6042c47a and b/3f743855-ec1b-4e76-808a-eafc6042c47a differ
diff --git a/889e2e74-6ec3-448b-ad35-feb2187d9d2b b/889e2e74-6ec3-448b-ad35-feb2187d9d2b
new file mode 100644
index 0000000..0ae16b3
Binary files /dev/null and b/889e2e74-6ec3-448b-ad35-feb2187d9d2b differ
diff --git a/a007422b-0b9d-4773-aba7-54b6dd2b4ceb b/a007422b-0b9d-4773-aba7-54b6dd2b4ceb
index 5df614e..831cb07 100644
Binary files a/a007422b-0b9d-4773-aba7-54b6dd2b4ceb and b/a007422b-0b9d-4773-aba7-54b6dd2b4ceb differ


git checkout dc9ee9 ./889e2e74-6ec3-448b-ad35-feb2187d9d2b
error: pathspec '889e2e74-6ec3-448b-ad35-feb2187d9d2b' did not match any file(s) known to git.

Upvotes: 1

Views: 183

Answers (2)

twalberg
twalberg

Reputation: 62479

git diff dc9ee9 compares the contents of the commit with SHA-1 dc9ee9 with your current working directory. The results of the diff for your specific file:

diff --git a/889e2e74-6ec3-448b-ad35-feb2187d9d2b b/889e2e74-6ec3-448b-ad35-feb2187d9d2b
new file mode 100644
index 0000000..0ae16b3
Binary files /dev/null and b/889e2e74-6ec3-448b-ad35-feb2187d9d2b differ

indicate that the file did not exist in commit dc9ee9, but it does in your current working directory. Thus checking that file out from that commit will fail.

In fact, from your git log output, it appears that the file in question was committed in the next commit, b2df50ecd14472a1c38d2cd2269898d4f3ab9604, so instead of git checkout dc9ee9 889e2e74-6ec3-448b-ad35-feb2187d9d2b, you should try this instead (note also the -- syntax to separate commit from pathnames:

git checkout b2df50 -- 889e2e74-6ec3-448b-ad35-feb2187d9d2b

Also, if b2df50 is your current HEAD, you could instead do this:

git reset --hard HEAD -- 889e2e74-6ec3-448b-ad35-feb2187d9d2b

Upvotes: 2

Femaref
Femaref

Reputation: 61497

You can use checkout only to get the current version from the current commit. If the file doesn't exist in the current commit, you wont get it. To see the file, checkout the commit:

git checkout <sha1id>

You can get the sha1id from git log.

If you only want that file, and replace the version of it in your current working tree, use

git checkout <sha1id> path

instead.

Upvotes: 2

Related Questions