Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get complete git commit hash from short hash? [duplicate]

Tags:

git

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?

like image 688
Usman Avatar asked Jan 18 '17 10:01

Usman


People also ask

How do I get the last commit hash?

# open the git config editor $ git config --global --edit # in the alias section, add ... [alias] lastcommit = rev-parse HEAD ... From here on, use git lastcommit to show the last commit's hash.

How do I view commit hash?

If you have the hash for a commit, you can use the git show command to display the changes for that single commit. The output is identical to each individual commit when using git log -p .

Is git Short commit hash unique?

The only requirement for a git short commit hash is that it's unique within that repository, the git client will use variable length (default: 7) while GitLab seems to always use 8 characters.

How are git commit hashes calculated?

In Git, get the tree hash with: git cat-file commit HEAD | head -n1. The commit hash by hashing the data you see with cat-file . This includes the tree object hash and commit information like author, time, commit message, and the parent commit hash if it's not the first commit.


2 Answers

git rev-parse will give you what you want.

$ git rev-parse 3cdd5d
3cdd5d19178a54d2e51b5098d43b57571241d0ab
like image 178
Joe Avatar answered Oct 22 '22 07:10

Joe


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)

like image 7
mamapitufo Avatar answered Oct 22 '22 07:10

mamapitufo