Lesson 10 of 60 – Git Repository Structure
17%

Git Repository Structure

A Git repository contains your project files along with a special .git directory. The .git directory stores the information Git needs to manage versions, commits, branches, references and other repository data.

Note: The .git directory is the heart of a Git repository. You normally should not manually modify its internal files.

1. What is Git Repository Structure?

Git repository structure describes the files and directories used by Git to manage a project.

A typical project contains your source files and a hidden .git directory.

2. Basic Repository Structure

my-project/
│
├── .git/
├── index.html
├── style.css
└── script.js

Here, .git contains Git's internal repository data, while the other files are project files.

3. The .git Directory

The .git directory is created when you run:

git init

It stores the information required by Git to manage the repository.

4. Why .git is Important

Without the .git directory, the project directory does not contain the local Git repository metadata.

Git uses this directory to maintain information about the repository and its history.

5. Working Directory

The working directory is the area where you work with your project files.

For example:

index.html
style.css
script.js

You create and modify files in the working directory.

6. Staging Area

The staging area contains changes that you have selected for the next commit.

You add files to the staging area using:

git add index.html

7. Git Repository

The Git repository stores committed project history.

When you create a commit, Git records the staged changes in the repository.

git commit -m "Initial commit"

8. Three Main Areas of Git

Working Directory
       ↓
Staging Area
       ↓
Git Repository

Changes normally move through these areas as you work with Git.

9. Working Directory Example

my-project/
├── index.html
├── style.css
└── script.js

When you edit these files, the changes first exist in your working directory.

10. Moving Changes to Staging

Use git add to move changes into the staging area.

git add index.html

You can then check the state with:

git status

11. Moving Changes to Repository

After staging changes, create a commit:

git commit -m "Update website"

The commit becomes part of the repository history.

12. HEAD in Git

HEAD is a special reference that normally points to the currently checked-out branch.

It helps Git identify the current position in the project history.

13. Branch References

Git stores references to branches in the repository. For example, a branch such as main points to a commit in the project history.

main → latest commit

14. Objects in Git

Git internally stores different types of objects to represent repository data.

Common Git object types include:

  • Blob
  • Tree
  • Commit
  • Tag

15. Blob Object

A blob object stores file content.

The filename itself is not stored inside the blob. Git uses other repository structures to associate content with filenames.

16. Tree Object

A tree object represents directory information. It connects filenames with the objects representing their contents.

17. Commit Object

A commit object represents a recorded point in the project's history.

A commit contains information such as the author, commit message and references to the project state.

18. Tag Object

A Git tag can be used to give a meaningful name to a specific point in repository history.

Tags are often used to identify releases or important versions.

19. The Objects Directory

Inside the .git directory, the objects directory stores Git objects.

These objects form an important part of Git's internal data model.

20. The Refs Directory

The refs directory contains references used by Git, including references associated with branches and tags.

These references help Git identify important points in repository history.

21. The HEAD File

The HEAD file is located inside the .git directory.

It identifies the current branch or commit reference used by the working tree.

22. The config File

The config file inside .git contains repository-specific configuration information.

For example, repository-specific settings and remote information can be stored here.

23. The index File

The index file is associated with Git's staging area.

When you use git add, Git updates the index to record what is prepared for the next commit.

24. Sample .git Directory

.git/
├── HEAD
├── config
├── index
├── objects/
├── refs/
└── ...

The exact contents can vary depending on the repository and Git version, but these are important parts to understand.

25. Checking Git Status

The easiest way to understand the current state of the repository is to use:

git status

It can show the current branch and changes that are staged, unstaged or untracked.

26. Viewing Commit History

Use the following command to view commits:

git log

The log helps you understand the history stored in the repository.

27. Do Not Edit .git Manually

The files inside .git are managed by Git. Beginners should normally use Git commands rather than manually editing these internal files.

Warning: Incorrect changes inside the .git directory can damage or confuse a repository.

28. Complete Repository Flow

Project Files
     ↓
Working Directory
     ↓
git add
     ↓
Staging Area
     ↓
git commit
     ↓
Repository History
     ↓
Branches / Tags / References

Understanding this flow makes it easier to understand how Git manages project changes.

29. Example Repository Structure

student-project/
│
├── .git/
│   ├── HEAD
│   ├── config
│   ├── index
│   ├── objects/
│   └── refs/
│
├── index.html
├── style.css
└── script.js

The project files are outside .git, while Git's repository information is stored inside .git.

30. Summary of Git Repository Structure

A Git repository consists of your project files and the hidden .git directory. The working directory contains the files you edit, the staging area contains selected changes, and the repository stores committed history.

Working Directory
        ↓
    git add
        ↓
Staging Area
        ↓
   git commit
        ↓
Git Repository

The .git directory contains important internal information such as repository configuration, references, objects, the index and HEAD information.

📌 Key Points

  • A Git repository contains project files and Git metadata.
  • The .git directory is created by git init.
  • The working directory is where you edit project files.
  • The staging area contains changes prepared for the next commit.
  • The repository stores committed project history.
  • HEAD identifies the current branch or commit reference.
  • refs contains references such as branches and tags.
  • objects stores Git objects.
  • The index represents the staging area.
  • The config file contains repository-specific configuration.
  • Git objects include blobs, trees, commits and tags.
  • Normally, you should not manually modify files inside .git.

🧠 Quick Quiz

Question: Which directory contains Git's internal repository information?