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
Root of Git's internal storage
Git's object database
Compressed repository data
Additional object information
Object storage using hash prefixes
Reference storage
Branch references
Tag references
Remote repository references
Points to current branch
Repository configuration
Staging area information
Scripts for automation
Run before commit is created
Run after commit is created
Repository information
Local ignore patterns
Reference history
History of HEAD updates
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. Create a new repository with git init
- 2. Create and commit a few files
- 3. Create a new branch
Tasks:
- 1. Explore the .git directory structure
- 2. Look at the contents of HEAD
- 3. Find your commits in objects/
- 4. Examine refs/heads/
- 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