switz
switz

Reputation: 25188

Get File or Folder from git commit hash

So let's say I have a hash ccf52ef35d7767c5b0f251542ab79608c50a3a56.

How do I use the git command line to get the contents of the file from the commit hash and file structure.

Also, how do I get a directory listing if the path is a directory, not a file. I do not need checking, just the two commands you would use for each.

I know this is the command: git log [<options>] [<since>..<until>] [[--] <path>...] and here is the page.

I tried git log ccf52ef35d7767c5b0f251542ab79608c50a3a56, but that just returned the generic git log (all commits). I don't even know how I would get the contents of the file, except for maybe piping it into an ls or cat.

Edit: Okay so I figured out git show --pretty="format:" --name-only a3da8bb. Is the best way to just grep out either a directory and cat the file from there? Edit 2: Saw the answer, thanks. Still looking to figure out how to cat a file.

Upvotes: 2

Views: 5563

Answers (2)

manojlds
manojlds

Reputation: 301147

You probably want:

git show --pretty="format:" --name-only <sha1 hash>

It will show the files added / modified / deleted in the commit. Instead of --name-only, you can use --name-status to show the names along with the status ( modifed, added..)

To get the contents of file, you can use:

git show <hash>:file

or

git cat-file -p <hash>:file

Upvotes: 6

switz
switz

Reputation: 25188

I figured it out. Pretty simple, in fact.

git show hash:path/to/file

Upvotes: 1

Related Questions