Git internals

Everything is just files

Within git, there’s no real magic going on - it’s all stored as files that you can explore in the .git directory. Interally, git utilises a simple key-value store, stored as objects.

The ref is the key, the object is the value.

You can directly manipulate git objects using the low-level git hash-object internal function

$ echo "secret thing" | git hash-object -w --stdin
7b90fa56ff6186a500b2aaac270d57fb4dc4c56a

and then recover it using git show

git show 7b90fa56ff6186a500b

secret thing
(END)

File storage in .git/objects/

You can directly inspect the files in their binary format, or blobs.

$ cat .git/objects/7b/90fa56ff6186a500b2aaac270d57fb4dc4c56a
xK??OR04f(NM.J-Q(???K?K??% 

The data here isn’t encrypted, it’s just in a format that needs decoding properly. To do that we can use the low-level object inspector git cat-file.

$ git cat-file -p 7b90fa56ff6186a500b2aaac270d57fb4dc4c56a
secret thing

File structure

.git/
  hooks/            < executable scripts for before/after git actions
  info/
  logs/             < stores links between refs
  objects/          < where the ref data is stored
  refs/
    heads/          < local branch refs
      develop       < develop branch tip ref
      master
    tags/
      3.13.2        < tag ref
    stash           < stash ref
  COMMIT_EDITMSG    < most recent commit message
  config            < repo config settings
  description
  FETCH_HEAD
  HEAD              < stores the current HEAD ref
  index             < similar to git status
  ORIG_HEAD
  packed-refs       < commonly accessed ref, branch tips, tags

Now we can touch on submodules