Git Behind the Scenes

Git is a version control system that tracks changes in files and folders by storing the project history internally.

Git Snapshots

  • A snapshot represents the state of the code at a specific time
  • Each snapshot is identified by a unique hash
  • Git does not store differences, it stores snapshots
  • Internally, Git stores data as objects in a key-value database

Git Objects (3 Musketeers)

1. Commit Object

  • Represents a snapshot
  • Stored inside the .git folder
  • Contains:

    • Tree object reference
    • Parent commit reference
    • Author & committer
    • Commit message

2. Tree Object

  • Represents directories and file structure
  • Contains:

    • File mode
    • File name
    • File hash

Everything is stored as key-value pairs in the tree object. The key is the file name and the value is the file hash.

3. Blob Object

  • tree object and contains the actual file content
  • Does not store file name or path

Helpful Git Internal Commands

git show -s --pretty=raw <commit-hash>   # View raw commit object
git ls-tree <tree-id>                    # View tree object contents
git show <blob-id>                       # View file content (blob)
git cat-file -p <commit-id>              # Inspect commit object