2

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.

2
  • A commit hash isn't unique for a single file, it's unique for a whole commit which can consist of multiple files. Commented Jan 21, 2012 at 7:15
  • Yes, I know. I'll have the directory structure and file name already. Commented Jan 21, 2012 at 7:19

2 Answers 2

6

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
Sign up to request clarification or add additional context in comments.

Comments

1

I figured it out. Pretty simple, in fact.

git show hash:path/to/file

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.