I have a short git commit hash of length 8 characters. I want to get a complete hash from remote server. I tried getting the branch name from the commit hash and then getting the full commit hash from branch name but it only works for the latest commit. What could be the best possible way to achieve what I want?
Asked
Active
Viewed 2.7k times
56
-
What do you mean by "from remote server"? Do you have a local clone of the repository? – Philipp Wendler Jan 18 '17 at 10:27
-
I do have a local clone of the repository but i need to do it before i fetch. (I am actually working on a python script that automatically does everything that's why i don't want to fetch everytime) – Usman Jan 18 '17 at 10:33
-
This question is obviously not a duplicate of "What does git rev-parse do?" If you already know about rev-parse, you don't have to ask "How to get commit hash". The other question is more like an answer than a duplicate question. – Christopher Hamkins Dec 19 '22 at 14:31
2 Answers
104
git rev-parse
will give you what you want.
$ git rev-parse 3cdd5d
3cdd5d19178a54d2e51b5098d43b57571241d0ab

Joe
- 29,416
- 12
- 68
- 88
-
Any way to do this for a commit that hasn't been fetched? I'd like to be able to get the full commit hash given a short commit hash on a remote repo. – Noel Yap Aug 04 '23 at 18:03
7
You can use the --pretty
option of the show
command:
$ git show --pretty=%H 62a0505
62a0505e8204115b8b9c8a95bfa264a8c0896a93
(assuming you have a local clone of the repo)

mamapitufo
- 4,680
- 2
- 25
- 19