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'.
Community
  • 1
  • 1
Odrai
  • 2,163
  • 2
  • 31
  • 62

1 Answers1

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.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • The application still retrieves: "fatal: Path 'D://Test//REPOFOLDER//' exists on disk, but not in 'a2f3f51ee27d5711d2974f0256b4eed0b3225d44'. " How to solve this issue? – Odrai Apr 17 '16 at 13:27
  • @Odrai What exact command did you type? What git version are you using (`git --version`)? – VonC Apr 17 '16 at 13:28
  • Git version 2.8.1.windows.1 and the command: "show a2f3f51ee27d5711d2974f0256b4eed0b3225d44:D://Test//MyRepoFolder// > test.cs" – Odrai Apr 17 '16 at 13:29
  • @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. – VonC Apr 17 '16 at 13:32
  • The same error still appears. Maybe we can use TeamViewer? – Odrai Apr 17 '16 at 13:42
  • Last question/ issue: The command works in Git CMD, but not in my C# application. **Command:** git show a2f3f51ee27d5711d2974f0256b4eed0b3225d44:test.css > anotherpath\test.css **Error:** "fatal: ambiguous argument '>': unknown revision or path not in the working tree. Use '--' to separate paths from revisions, like this: 'git [...] -- [...]' " – Odrai Apr 17 '16 at 14:16
  • @Odrai First, is test.css right under D:\test\ReactiveUI? – VonC Apr 17 '16 at 14:17
  • @Odrai Second, can you try and use the --, as requested? `git show a2f3f51ee27d5711d2974f0256b4eed0b3225d44:test.css --` – VonC Apr 17 '16 at 14:17
  • I tried git show a2f3f51ee27d5711d2974f0256b4eed0b3225d44:ReactiveUI/ExpressionMixins.cs > D:/Downloads/OLDTEST.cs **AND** show a2f3f51ee27d5711d2974f0256b4eed0b3225d44:ReactiveUI/ExpressionMixins.cs > --D:/Downloads/OLDTEST.cs **AND** show a2f3f51ee27d5711d2974f0256b4eed0b3225d44:ReactiveUI/ExpressionMixins.cs > --D:/Downloads/OLDTEST.cs – Odrai Apr 17 '16 at 14:20
  • @Odrai the '`--`' is only for the git command, it does not involve whatever is after the `>`. So `git show a2f3f51ee27d5711d2974f0256b4eed0b3225d44:ReactiveUI/ExpressionMixins.cs -- > D:/Downloads/OLDTEST.cs` – VonC Apr 17 '16 at 14:22
  • New error: "fatal: D:/Downloads/OLDTEST.cs: 'D:/Downloads/OLDTEST.cs' is outside repository\n" – Odrai Apr 17 '16 at 14:25
  • @Odrai ok, it consider the rest as part of the git command: not good. `git show a2f3f51ee27d5711d2974f0256b4eed0b3225d44:ReactiveUI/ExpressionMixins.cs > D:/Downloads/OLDTEST.cs` should work, but if it does not, test of the redirection itself is possible: `echo ok > D:/Downloads/OLDTEST.cs` (within your project execution), just to test if that work. – VonC Apr 17 '16 at 14:28
  • Can't execute that: "git: 'echo' is not a git command. See 'git --help'.\n\nDid you mean this?\n\tfetch\n" – Odrai Apr 17 '16 at 14:31
  • @Odrai then `git --version`, for instance – VonC Apr 17 '16 at 14:32
  • git --version result is "git version 2.8.1.windows.1\n" within the application. – Odrai Apr 17 '16 at 14:35
  • @Odrai has it been redirected? – VonC Apr 17 '16 at 14:35
  • Apparently my application doesn't support ' > ' that well. So I will use the command without ' > ' and export the content to a destination file. Thanks for helping @VonC – Odrai Apr 17 '16 at 14:51
  • 1
    @Odrai Agreed. I have amended the answer to reflect that. – VonC Apr 17 '16 at 14:53
  • At this moment I am using the next command in my application -C D:\ProjectsFolder\Test2\ReactiveUI log D:/ProjectsFolder/Test2/ReactiveUI/ReactiveUI/Activation.cs, but if e.g. ProjectsFolder contains a space -> "Projects Folder" an error occurs: fatal: Invalid object name 'D'. I already tried -C D:\"Projects Folder"\Test2\Reactive... but the same error appears. Maybe you can point me in the right direction? – Odrai May 04 '16 at 17:57
  • @Odrai Did you try `-C "D:\Projects Folder\Test2\ReactiveUI"`? – VonC May 04 '16 at 18:00
  • @Odrai what OS, what system are you using? What is the exact command line you are typing? – VonC May 04 '16 at 18:18
  • @Odrai At this point, it would be best to ask a new question with as much details as possible (and a link back to this one for context) – VonC May 04 '16 at 19:15