Git Repository Structure

Understanding how Git organizes and stores your project data

Learning Objectives

  • Understand the structure and purpose of the .git directory
  • Learn how Git stores objects and manages references
  • Explore the basic Git architecture and its components
  • Discover how Git tracks and maintains your project history

The Git Repository Structure

When you initialize a Git repository with git init, Git creates a hidden .git directory. This directory contains everything Git needs to track and manage your project. Understanding its structure helps you work more effectively with Git.

Git Repository Structure

Click on items to explore the repository structure

.git/

Root of Git's internal storage

objects/

Git's object database

pack/

Compressed repository data

info/

Additional object information

[sha1]/

Object storage using hash prefixes

refs/

Reference storage

heads/

Branch references

tags/

Tag references

remotes/

Remote repository references

HEAD

Points to current branch

config

Repository configuration

index

Staging area information

hooks/

Scripts for automation

pre-commit

Run before commit is created

post-commit

Run after commit is created

info/

Repository information

exclude

Local ignore patterns

logs/

Reference history

HEAD

History of HEAD updates

refs/

Branch update history

Click on the directories and files above to explore the Git repository structure. Each item shows its purpose and relationship to other components.

How Git Stores Objects

Understanding how Git stores objects internally helps you work with Git more effectively and troubleshoot issues when they arise. Git uses a content-addressable filesystem, which means every object is stored based on its content's SHA-1 hash.

Object Types

  • Blobs

    Store file contents. Each version of a file is stored as a separate blob.

  • Trees

    Store directory structures and references to blobs and other trees.

  • Commits

    Store commit metadata and point to a tree representing the project state.

  • Tags

    Store additional information for annotated tags.

Object Storage

Location

Objects are stored in .git/objects/ using their SHA-1 hash as the filename.

Structure

First two characters of hash form directory name, remaining characters form file name.

Content

Objects are compressed using zlib compression to save space.

Practice Exercise

Let's explore the Git repository structure with a hands-on exercise:

Exercise: Exploring Repository Structure

Setup:

  1. 1. Create a new repository with git init
  2. 2. Create and commit a few files
  3. 3. Create a new branch

Tasks:

  1. 1. Explore the .git directory structure
  2. 2. Look at the contents of HEAD
  3. 3. Find your commits in objects/
  4. 4. Examine refs/heads/
  5. 5. Create a tag and find it in refs/tags/

Success Criteria:

  • • Understand the basic .git directory structure
  • • Locate different types of Git objects
  • • Find and interpret references

Knowledge Check

Before moving on to the next lesson, make sure you can answer these questions:

  • 1. What are the four types of objects in Git's object database?

    Think about how Git stores different types of data.

  • 2. What is the purpose of the HEAD reference?

    Consider how Git keeps track of your current location.

  • 3. How does Git store directory structures?

    Think about the relationship between tree and blob objects.

  • 4. What's the difference between tags and branches in terms of references?

    Consider how these references behave when new commits are made.

What's Next?

Now that you understand Git's repository structure, we'll move on to basic Git operations. In the next section, you'll learn about:

  • Creating your first Git repository
  • Basic commands for tracking files
  • Making your first commits