2

I have a Git repository and I would like to see how a file looked at a specific commit.

So to retrieve all commits of a file, I am using git log [filepath]. With git log, the application retrieves the commits and their SHA values.

What is/ are the Git command(s) to retrieve the file of a specific commit?

So in pseudo code:

  1. Get all commits of a file (already implemented)
  2. Retrieve file of a commit SHA
  3. Export file to a specific folder.

I already tried:

git show [SHA OF COMMIT] -- [REPODIRECTORY]
git show [SHA OF COMMIT]:[FILEPATH] > [NEW LOCATON]

That gave:

Error: fatal: Path 'D://Test//FolderName//FolderName//MyTestFile.cs' exists on disk, 
but not in 'a2f3f51ee27d5711d2974f0256b4eed0b3225d44'.

1 Answer 1

2

git show [SHA OF COMMIT]:[FILEPATH] > [NEW LOCATON] should be the right syntax.

But [FILEPATH] should be the path from the root of the repo, not the absolute file path form the host drive disk.

From gitrevisions man page:

<rev>:<path>, e.g. HEAD:README, :README, master:./README

A suffix : followed by a path names the blob or tree at the given path in the tree-ish object named by the part before the colon.
:path (with an empty part before the colon) is a special case of the syntax described next: content recorded in the index at the given path.
A path starting with ./ or ../ is relative to the current working directory. The given path will be converted to be relative to the working tree’s root directory.


In your case:

cd /d D:\Test\MyRepoFolder
git show a2f3f51ee27d5711d2974f0256b4eed0b3225d44:test.css > anotherpath\test.css

You would use :test.css, not :D://... (the absolute file path).
The root of the repo is "D:\Test\MyRepoFolder".
Hence, the path to access test.css should relative to that root.

In the OP's instance, the exact path of the file was ReactiveUI\afile, and the repo was in D:\Test\ReactiveUI hence a bit of confusion.

Even though the root folder was named ReactiveUI, the command to use was:

git show <sha1>:ReactiveUI/afile

(with '/', not '\', as it is interpreted by the Linux git bash)

That is, "the path relative to the root folder of the repo".

Note: the ProcessInfo class used by the OP does not support well the '>'.
The export of the git command output has to be done programmatically.

Sign up to request clarification or add additional context in comments.

22 Comments

The application still retrieves: "fatal: Path 'D://Test//REPOFOLDER//' exists on disk, but not in 'a2f3f51ee27d5711d2974f0256b4eed0b3225d44'. " How to solve this issue?
@Odrai What exact command did you type? What git version are you using (git --version)?
Git version 2.8.1.windows.1 and the command: "show a2f3f51ee27d5711d2974f0256b4eed0b3225d44:D://Test//MyRepoFolder// > test.cs"
@Odrai I have edited my answer to illustrate what I meant by "should be the path from the root of the repo" in your case.
@Odrai Agreed. I have amended the answer to reflect that.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.