Search code examples
gitgit-diffgit-checkoutgit-show

How to get the SHA of commit from diff output?


How can I get the SHA of the commit from diff output?

For example I like to diff a binary file, the output of git show COMMIT is:

diff --git a/0_prospektusok/FAR_feltetdiszek/feltetdisz_prospektus.xls b/0_prosp
index 9993010..707c169 100644
Binary files a/0_prospektusok/FAR_feltetdiszek/feltetdisz_prospektus.xls and b/0

The git show 9993010 shows the file on terminal, but if I redirecting it to a file and opening with MS Excel, it contains junk.

The git checkout 9993010 says fatal: reference is not a tree: 9993010.

How can I checkout the versions of a and b?


Solution

  • If you want to checkout the whole repo as it was before COMMIT use git checkout COMMIT~.
    If you want to keep your working copy and all but only update the file to the state that it had before COMMIT, use git checkout COMMIT~ -- 0_prospektusok/FAR_feltetdiszek/feltetdisz_prospektus.xls.
    For explanation why git checkout 9993010 didn't work, read the answer to How does "index f2e4113..d4b9bfc 100644" in git diff correspond to SHA1 ID in gitk?