Understanding how Git organizes and stores your project data
Deep dive into Git repository structure, .git directory, and how Git organizes your project files.
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.
Click on the directories and files above to explore the Git repository structure. Each item shows its purpose and relationship to other components.
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.
- #### 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.
Objects are stored in .git/objects/ using their SHA-1 hash as the filename.
First two characters of hash form directory name, remaining characters form file name.
Objects are compressed using zlib compression to save space.
Let's explore the Git repository structure with a hands-on exercise:
- Create a new repository with git init
- Create and commit a few files
- Create a new branch
- Explore the .git directory structure
- Look at the contents of HEAD
- Find your commits in objects/
- Examine refs/heads/
- Create a tag and find it in refs/tags/
- Understand the basic .git directory structure
- Locate different types of Git objects
- Find and interpret references
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.
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