Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a tree and commit type in Git?

Tags:

git

I checked in one file into a simple git repo. From my investigations; there are three types of objects placed in .git/objects

  1. commit
  2. tree
  3. blob

As an example:

$ git cat-file -t 8b4e834eba22e60c284c7b77e43d3c29e619f92f
commit

$ git cat-file -t c7c5b03aea0b8c970c93de3670c28f2108948266
tree

$ git cat-file -t e965047ad7c57865823c7d992b1d046ea66edf78
blob

If I tried to run git-ls-tree on a blob, it throws an error.

But it is equally possible for me to run it on a commit or tree object.

$ git ls-tree -t c7c5b03aea0b8c970c93de3670c28f2108948266
100644 blob e965047ad7c57865823c7d992b1d046ea66edf78    readme.txt

$ git ls-tree -t 8b4e834eba22e60c284c7b77e43d3c29e619f92f
100644 blob e965047ad7c57865823c7d992b1d046ea66edf78    readme.txt

Is a commit object also a tree ? What are their differences, if any ?

like image 716
Frankie Ribery Avatar asked Mar 09 '11 07:03

Frankie Ribery


1 Answers

A commit object is an object that references a tree and associates other metadata (author, committer, timestamps, etc) with it.

         Commit
       /        \
      /          \
(parent SHA)     Tree
(author)        /    \
(committer)   Blob   Blob
(timestamps)
(etc)

Multiple commit objects can reference the same tree (if the state of files in each commit is identical).

For instance, if two people make the same change to a file and commit, it will result in two different commit objects (since both the timestamps and the authors will differ). However, since the end result is the same file contents, both commits will point to the same tree.

When you run git ls-tree on a commit SHA, it automatically uses the tree SHA referenced by that commit instead.

like image 115
Amber Avatar answered Oct 23 '22 06:10

Amber