Git's anatomy consists of three main layers: the working tree, the staging index (or just "index"), and the commit history (HEAD). You edit files in the working tree, use git add to stage changes to the index, and then use git commit to permanently save that staged state to the repository's history. This three-tiered structure is what gives you precise control over what gets recorded in each snapshot of your project.

What Are the Three Layers of Git?

The three layers form the core workflow for tracking changes. The working tree is your project's directory on your computer where you create, edit, and delete files. These are your current, active changes that are not yet being tracked by Git for the next commit. The staging index is a hidden area, often called "staging," that acts as a preview of your next commit. You selectively add changes from the working tree to this layer using the git add command. Finally, the HEAD is a pointer that refers to the most recent commit on your current branch, representing the last known good state of your project that is permanently stored in the repository's history.

Think of it like packing a box for moving. Your entire house is the working tree, full of items. You choose specific items and place them into a cardboard box, which is the staging index. Once the box is full and organized to your liking, you tape it shut, label it, and put it on the truck—this final act is the git commit, which creates a new permanent state in the history (the moving truck). The label on the box you just packed is the new HEAD.

How Do You Move Changes Between the Layers?

You move changes between these layers using specific Git commands. To see what has changed in your working tree compared to the last commit (HEAD), you use git status and git diff. The git status command shows you which files are modified but not staged and which files are staged and ready for commit. The git diff command shows you the exact line-by-line changes in your working tree that are not yet staged.

To promote changes from the working tree to the staging index, you use the git add <file> command. You can add a specific file or use git add . to add all changes in the current directory and its subdirectories. To see what is currently in the staging index and will be part of the next commit, you use git diff --staged (or git diff --cached; they are the same command). This shows you the difference between the staged changes and the last commit.

The final step is to permanently save the contents of the staging index to the repository's history. You do this with the git commit command. Running git commit -m "Your message here" takes everything in the staging index, packages it into a new commit object, and moves the HEAD pointer to this new commit. This action clears the staging index, making it ready to accept a new set of changes for the next commit cycle.

How Do You See the State of Each Layer?

Git provides commands to inspect each layer independently. For the working tree, the primary command is git status. It will list files under two headings: "Changes not staged for commit" (modified files still only in the working tree) and "Untracked files" (new files that Git has never seen before). The git diff command with no arguments shows you the line-by-line differences between your working tree and the staging index.

To see the content of the staging index, you use git diff --staged. This shows you what you have already added with git add and what will be included in the next commit if you run git commit right now. To see the history of commits, which is the chain of HEAD pointers, you use git log. This command shows you the author, date, and message for each commit, providing a narrative of the project's development. The git log --oneline flag gives you a condensed view for a quick overview.

What Are Common Mistakes with Git's Layers?

A common mistake is confusing the working tree and the staging index. Developers often make edits and then immediately run git commit, forgetting that git add is a necessary middle step. This results in the commit only containing changes that were previously staged, not the latest edits made in the working tree. Another frequent error is using git add . without carefully reviewing what it includes, which can accidentally stage debugging code, log files, or temporary documents that should not be part of the repository.

Users sometimes misunderstand what git reset does. A hard reset (git reset --hard HEAD) discards all changes in both the working tree and the staging index, moving everything back to match the HEAD commit. This is destructive and cannot be undone if the changes were not committed. A mixed reset (git reset HEAD, which is the default) unstages changes from the staging index but leaves them as modifications in your working tree, allowing you to re-stage them more carefully.

How Do You Recover from Mistakes Between Layers?

If you have staged a file incorrectly with git add, you can unstage it while keeping the changes in your working tree by using git reset HEAD <file>. This command moves the file's changes from the staging index back to the working tree. The changes are still present in your files, but they are no longer marked for the next commit. This is the correct way to fix a mistaken git add without losing your work.

If you have made changes in your working tree that you want to completely discard, reverting a file to exactly how it appears in the last commit (HEAD), you can use git checkout -- <file>. This command will overwrite the file in your working tree with the version from HEAD, permanently erasing any local modifications you made to that file. It is a dangerous command, so use it only when you are certain you do not need those changes.

When Should You Commit from the Staging Index?

You should create a commit when the changes in your staging index represent a single, logical unit of work. A good commit focuses on one thing: fixing a specific bug, adding a new feature, or refactoring a particular module. This practice creates a clean, readable history. If your staging index contains changes for multiple unrelated tasks, it is better to unstage some changes using git reset HEAD <file> and then commit the related changes separately.

Before committing, always double-check what is in the staging index by running git diff --staged. This quick review ensures you are not accidentally committing sensitive data like passwords, API keys, or large binary files that should be excluded via the .gitignore file. It also confirms that the commit contains exactly the changes you intend to record, making your project's history a reliable record of development.

Git Layers for Partial Changes and Patches

Git allows you to stage even parts of a file, not just entire files. This is incredibly useful for separating unrelated changes made to the same file. After modifying a file, you can use git add -p <file> (the -p flag stands for "patch"). Git will then interactively walk you through each "hunk" of changes in the file and ask if you want to stage it or not. This lets you craft precise commits that contain only the relevant edits for a specific task.

This interactive staging process is a powerful way to keep your commits clean. For example, if you fixed two separate bugs in the same file, you can use git add -p to stage the lines for the first bug, commit them with a message like "Fix login timeout error," and then stage the lines for the second bug and commit them with a different message. This results in a history where each commit has a clear and singular purpose, which is invaluable for debugging and understanding past decisions.