11

Today I'm learning some basic git knowledge via reading this doc online:

http://git-scm.com/book/en/v2/Git-Basics-Viewing-the-Commit-Hi

And at that chapter, I'm start to learn using git log --pretty=format:" " to show log info at my taste.

But some how, I saw in the format table two similar options, %H for Commit Hash, %P for Parent Hash and %T for Tree Hash.

I experimented them on my command line, it comes out they are all hash values of same length with different value.

I googled and stackoverflowed, no obvious hints so far.

I have idea about this Hash value, it's a checksum of that git commit.

But what does Parent Hash and Tree hash do?

  • PS: Ah, I got some ideas now, did the Parent Hash mean the hash value of the directly origin of a branch?
5

Parent hashes:

$ git log --graph
*   commit c06c4c912dbd9ee377d14ec8ebe2847cf1a3ec7e
|\  Merge: 79e6924 3113760
| | Author: linjie <linjielig@gmail.com>
| | Date:   Mon Mar 14 16:02:09 2016 +0800
| |
| |     commit5
| |
| |     Merge branch 'dev'
| |
| * commit 31137606f85d8960fa1640d0881682a081ffa9d0
| | Author: linjie <linjielig@gmail.com>
| | Date:   Mon Mar 14 16:01:26 2016 +0800
| |
| |     commit3
| |
* | commit 79e69240ccd218d49d78a72f33002fd6bc62f407
|/  Author: linjie <linjielig@gmail.com>
|   Date:   Mon Mar 14 16:01:59 2016 +0800
|
|       commit4
|
* commit 7fd4e3fdddb89858d925a89767ec62985ba07f3d
| Author: linjie <linjielig@gmail.com>
| Date:   Mon Mar 14 16:01:00 2016 +0800
|
|     commit2
|
* commit 316dd3fb3c7b501bc9974676adcf558a18508dd4
  Author: linjie <linjielig@gmail.com>
  Date:   Mon Mar 14 16:00:34 2016 +0800

     commit1

$ git log --pretty=format:'%<(82)%P %s'
79e69240ccd218d49d78a72f33002fd6bc62f407 31137606f85d8960fa1640d0881682a081ffa9d0  commit5
7fd4e3fdddb89858d925a89767ec62985ba07f3d                                           commit4
7fd4e3fdddb89858d925a89767ec62985ba07f3d                                           commit3
316dd3fb3c7b501bc9974676adcf558a18508dd4                                           commit2
                                                                                   commit1

You can see commit4 and commit3 is parent of commit5, commit2 is parent of commit3 and commit4, commit1 is parent of commit2.

Tree hash:

$ git log --pretty=format:'%T %s'
f3c7cee96f33938631a9b023ccf5d8743b00db0e commit5
e0ecb42ae45ddc91c947289f928ea5085c70b208 commit4
d466aea17dc07516c449c58a73b2dc3faa9d11a1 commit3
b39f2e707050e0c5bbb3b48680f416ef05b179ba commit2
5706ec2b32605e27fa04cbef37d582325d14dda9 commit1

$ git cat-file -p f3c7ce
100644 blob 8bb2e871e94c486a867f5cfcbc6f30d004f6a9e5    dev
100644 blob 47f16c8e00adba77ec5c176876e99c8e9f05d69b    master

$ git cat-file -p 5706ec
100644 blob fc0bfde0d44bb4d6c7d27b6e587ebedd34ba5911    master

The command's function: Pretty-print the contents of <object> based on it's type.

git cat-file -p 

In git,all the content is stored as tree and blob objects, with trees corresponding to UNIX directory entries and blobs corresponding more or less to inodes or file contents. A single tree object contains one or more tree entries, each of which contains a SHA-1 pointer to a blob or subtree with its associated mode, type, and filename. Git normally creates a tree by taking the state of your staging area or index and writing a series of tree objects from it. Commit objects have the information about who saved the tree object, when they saved or why they were saved. This is the basic information that the commit object stores for you.

Conclusion:

Commit hash, Parent hash, Tree hash are all SHA-1. Commit hash and Parent hash is identical except Parent hash has child. Tree hash is represent a Tree object. Commit hash and Parent hash represent a commit object.

Reference:

  1. Git Internals - Git Objects

  2. git-cat-file - Provide content or type and size information for repository objects

2

"Commit Hash" is the hash for the current commit. The commit the entry is associated with.

"Parent Hash" is the hash for any parent branch(es) the commit comes from.

"Tree hash" is the hash of the current directory in the commit. The hash is equal to the hash the directory has if seen from the parent directory with git ls-files --stage --abbrev.

Reference:

  • 1
    what does commit has mean in the beginning of your 3rd paragraph? – Zen Nov 1 '14 at 4:51
2

A tree is a hierarchical collection of files and directories, not tied to any particular point in history. For example, if you create a file and then later delete the file (with no other intervening commits), you will end up with the same tree you started with.

A commit is a point in the history of your project. A commit specifies a tree, but also contains other information such as author/committer and time, a commit message (in which the author describes what changed), and most importantly zero or more parents, which are the previous state of the repository. (Your very first commit has zero parents. Most commits after that have one parent during linear development, and more than one if you merge.)

You can get a sense of how this works with the git cat-file -p command, which prints the contents of a particular hash, regardless of the type. For example, to look at the HEAD commit, you can run:

$ git cat-file -p HEAD
tree 81ca1cb660ea79131336944df28b13b711d93557
parent 92b6b8fe9956866ace5397e060e7cc8ee1c76233
parent 7ea2575ed96d150ee19f70edea4bd42c7c2f0b83
author Mislav MarohniÄ <mislav.marohnic@gmail.com> 1436468108 -0700
committer Mislav MarohniÄ <mislav.marohnic@gmail.com> 1436468108 -0700

Merge pull request #951 from github/global-args

Avoid depending on a hardcoded list of git global flags

To see the tree inside that commit, you can cat-file -p it's tree:

$ git cat-file -p 81ca1cb660ea79131336944df28b13b711d93557
100644 blob 730f77a3be502cfe6769c1305c0b59c22274caf5        .gitignore
100644 blob bcbd000f6b9ad5b0510f804ac4a3b19306b39c03        .travis.yml
100644 blob da71aa1fa3c3ae47b2fe5e6245ce2eea1586e278        CONTRIBUTING.md
...

Similarly, if you look at the parents, you will see those are commits as well. A shorthand for the tree inside a commit such as rev is rev^{tree}. So the previous command could have been written git cat-file -p HEAD^{tree}. Note that rev^ stands for the parent of rev. When there are multiple parents, rev^1, rev^2, etc. More information is available in the git rev-parse man page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.