Git Object Store: Creating the Sample Working Directory

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 srcsrc$ cat > hello.cmain(){ printf("Hello worldn");}src$ cat > hello.py#!/usr/bin/pythonprint "Hello world."src$ cd ../docs docs$ lsdocs$ cat > helloThehelloprogramspeakstotheworld.docs$ lshellodocs$ cd ..$ lsdocs/ 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 --stage100644 bccdfbd6314e19a21c367ba5ea9cbe65a1a0818e 0 README100644 ddd8641d67317cf6dfd9e456d4bee2dc9a1d15b0 0 docs/hello100644 6403d7d93240a0a132644188d340039f6bcc6469 0 src/hello.c100644 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 --abbrev100644 bccdfbd 0 README100644 ddd8641 0 docs/hello100644 6403d7d 0 src/hello.c100644 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 HEADtree 39179a1d15da96b6594a87eac3bac631a6c728e8author Tim Flagg <me@gitguys.com> 1297723281 -0800committer Tim Flagg <me@gitguys.com> 1297723281 -0800Initial 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 --abbrev100644 blob bccdfbd README040000 tree 31b11e8 docs040000 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 --abbrev100644 blob ddd8641 hello$ git ls-tree 05f8c83 --abbrev100644 blob 6403d7d hello.c100644 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:

The Git Tree Object
The Git Commit Object
The Git Tag Object
The Git Blob Object
All Git Object Types: Blob, Tree, Commit And Tag
What Is The Format Of A Git Blob?

Recent Articles

Where Do I Get Git?

Where do you get git? We have the answer to that for Linux (Ubuntu, Debian, and Red Hat), Windows, and Apple OS as well as over 30 free git tutorials.

What is the deal with the Git Index? What is the Git Index?

The git "index" is where you place files you want committed to the git repository. Learn more about the git index and git commit by selecting this link.

What Every VCS Must do

Version Control System ("VCS") need to be able to provide to its users? GitGuys explains exactly what it needs to include and provides tutorials on Git.

Visualising Branches And Their Commits

We explain and discuss the use of the git show-branch, gitg, gitk and qgit in this Tutorial. Complete with examples.

“Tracking Branches” And “Remote-Tracking Branches”

Both Tracking Branches and Remote Tracking Branches are created on the git client when a git clone command is used.

Related Stories

Leave A Reply

Please enter your comment!
Please enter your name here

Stay on op - Ge the daily news in your inbox