Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

simplest way to get the SHA of an arbitrary git object?

Tags:

git

I want a git command that simply outputs the SHA of an arbitrary object (commit, tree, blob, whatever). Basically:

$ git sha HEAD
7b78f727c91edc7726f3c31113bc7b1509fea163
$ git sha master^:CHANGELOG
0dcc5f003ed89c30a8d0376a29d546c20449fd90

...and so forth. This should be the simplest thing in the world, but I can't figure out a good way of doing it. I don't mind using an alias to a complex, option-laden git command, but it seems like I must just be missing a really simple dereference command that git must surely use under the hood all the time.

like image 885
James MacAulay Avatar asked Jul 09 '10 16:07

James MacAulay


People also ask

Which objects in Git have a SHA1 hash?

Since trees and blobs, like all other objects, are named by the SHA1 hash of their contents, two trees have the same SHA1 name if and only if their contents (including, recursively, the contents of all subdirectories) are identical.

What is SHA in Git?

As is well-known, Git has been using SHA-1 to calculate a hash for each commit: For example, files, directories, and revisions are referred to by hash values unlike in other traditional version control systems where files or versions are referred to via sequential numbers.

What is a Git blob?

Blob is an abbreviation for “binary large object”. When we git add a file such as example_file. txt , git creates a blob object containing the contents of the file. Blobs are therefore the git object type for storing files.

What is a tree object in Git?

A "tree" in Git is an object (a file, really) which contains a list of pointers to blobs or other trees. Each line in the tree object's file contains a pointer (the object's hash) to one such object (tree or blob), while also providing the mode, object type, and a name for the file or directory.


1 Answers

For the purposes you have given, your git sha is actually git rev-parse.

% git rev-parse HEAD
47753f420d6ec7d84f8705e9acb67693745b4a8b
% git rev-parse origin/pu^:Documentation/RelNotes-1.7.1.txt
9d89fedb36b4d6fa7c8a6a8487cc47b4ca542e3a
like image 164
Chris Johnsen Avatar answered Sep 29 '22 00:09

Chris Johnsen