Git Internals:

The .git directory

Goals

  • Learn about the structure of the .git directory

The .git Directory

Time to do some exploring. First, from the root of your project directory…

Execute:

  1. ls -C .git

Output:

  1. $ ls -C .git
  2. COMMIT_EDITMSG config index objects
  3. HEAD description info refs
  4. ORIG_HEAD hooks logs

This is the magic directory where all the git “stuff” is stored. Let’s peek in the objects directory.

The Object Store

Execute:

  1. ls -C .git/objects

Output:

  1. $ ls -C .git/objects
  2. 09 27 42 59 6b 7a 9c b5 e4 pack
  3. 11 28 43 5a 72 90 af c4 e7
  4. 24 30 44 69 78 97 b0 c8 info

You should see a bunch of directories with 2 letter names. The directory names are the first two letters of the sha1 hash of the object stored in git.

Deeper into the Object Store

Execute:

  1. ls -C .git/objects/<dir>

Output:

  1. $ ls -C .git/objects/09
  2. 6b74c56bfc6b40e754fc0725b8c70b2038b91e 9fb6f9d3a104feb32fcac22354c4d0e8a182c1

Look in one of the two-letter directories. You should see some files with 38-character names. These are the files that contain the objects stored in git. These files are compressed and encoded, so looking at their contents directly won’t be very helpful, but we will take a closer look in a bit.

Config File

Execute:

  1. cat .git/config

Output:

  1. $ cat .git/config
  2. [core]
  3. repositoryformatversion = 0
  4. filemode = true
  5. bare = false
  6. logallrefupdates = true
  7. ignorecase = true
  8. precomposeunicode = true
  9. [user]
  10. name = Jim Weirich
  11. email = jim (at) edgecase.com

This is a project-specific configuration file. Config entries in here will override the config entries in the .gitconfig file in your home directory, at least for this project.

Branches and Tags

Execute:

  1. ls .git/refs
  2. ls .git/refs/heads
  3. ls .git/refs/tags
  4. cat .git/refs/tags/v1

Output:

  1. $ ls .git/refs
  2. heads
  3. tags
  4. $ ls .git/refs/heads
  5. master
  6. $ ls .git/refs/tags
  7. v1
  8. v1-beta
  9. $ cat .git/refs/tags/v1
  10. 4254c9466673a6ddbeb35e2802315dbcfc1dbb62

You should recognize the files in the tags subdirectory. Each file corresponds to a tag you created with the git tag command earlier. Its content is just the hash of the commit tied to the tag.

The heads directory is similar, but is used for branches rather than tags. We only have one branch at the moment, so all you will see is master in this directory.

The HEAD File

Execute:

  1. cat .git/HEAD

Output:

  1. $ cat .git/HEAD
  2. ref: refs/heads/master

The HEAD file contains a reference to the current branch. It should be a reference to master at this point.