Creating the sample working directory and placing it into the git object store

Git Object Store – Additional Directions

The below shows how the files and directories were created and added to the git object store that is discussed in Git object: Tree.

Create the files that are shown in The Git Object Tree diagram:

$ echo This is the README file. > README
$ mkdir src docs
$ cd src
src$ cat > hello.c
main()
{
  printf("Hello worldn");
}
src$ cat > hello.py
#!/usr/bin/python
print "Hello world."
src$ cd ../docs 
docs$ ls
docs$ cat > hello
The
hello
program
speaks
to
the
world.
docs$ ls
hello
docs$ cd ..
$ ls
docs/  README  src/

Add all of the files that are in our working directory to git:

$ git add .
$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached ..." to unstage)
#
#	new file:   README
#	new file:   docs/hello
#	new file:   src/hello.c
#	new file:   src/hello.py
#

Commit all of the files to the git repository:

$ git commit -m"Initial commit"
[master (root-commit) 126af20] Initial commit
 Committer: Tim Flagg <me@gitguys.com>

 4 files changed, 15 insertions(+), 0 deletions(-)
 create mode 100644 README
 create mode 100644 docs/hello
 create mode 100644 src/hello.c
 create mode 100644 src/hello.py

Find out out the hashes of the files (though this does not display the hashes of the trees):

$ git ls-files --stage
100644 bccdfbd6314e19a21c367ba5ea9cbe65a1a0818e 0   README
100644 ddd8641d67317cf6dfd9e456d4bee2dc9a1d15b0 0   docs/hello
100644 6403d7d93240a0a132644188d340039f6bcc6469 0   src/hello.c
100644 dd7a98c6cc515373a086f6829bd6f0f45481d569 0   src/hello.py

Let’s do that again, but this time we’ll ask for the hash to be abbreviated to just the first few characters (which is almost always unique enough to git when referring to a hash):

$ git ls-files --stage --abbrev
100644 bccdfbd 0    README
100644 ddd8641 0    docs/hello
100644 6403d7d 0    src/hello.c
100644 dd7a98c 0    src/hello.py


What is the hash of the top of the git tree shown in the git object store diagram? First we’ll display the most recent commit/check-in and find the git tree it refers to:

$ git cat-file -p HEAD
tree 39179a1d15da96b6594a87eac3bac631a6c728e8
author Tim Flagg <me@gitguys.com> 1297723281 -0800
committer Tim Flagg <me@gitguys.com> 1297723281 -0800

Initial commit

The hash of the top of the git tree is listed as 39179a. This matches what is shown on Diagram 2 (click on the
second thumbnail to see Diagram 2).

We can then use the git ls-tree command to see the hashes of the blobs and trees referred to from the top of the git tree:

$ git ls-tree 39179a --abbrev
100644 blob bccdfbd README
040000 tree 31b11e8 docs
040000 tree 05f8c83 src

The above output from git ls-tree shows there is 1 blob and 2 trees.

The only blob listed above is for the README file with the hash bccdfbd. You can compare that hash of bccdfbd with the hash for README shown by the git ls-files –stage –abbrev command, above. Sure enough, they match, so they are referring to the same file.

The other 2 objects shown in the output of the git ls-tree 39179a command are both trees, for the docs and src directories. You can see the contents of the docs and src trees by using the git ls-tree commands, using the hashes listed next to the docs and src trees, above.

$ git ls-tree 31b11e8 --abbrev
100644 blob ddd8641 hello
$ git ls-tree 05f8c83 --abbrev
100644 blob 6403d7d hello.c
100644 blob dd7a98c hello.py

The tree with hash 31b11e8, for the docs directory, lists the blob hello.

The tree with hash 05f8c83, for the src directory, lists the 2 blobs hello.c and hello.py.

Next: The Git Commit Object
Previous: The Git Blob Object

Related: