The anatomy of Git consists of three distinct areas—the working directory, the staging area (index), and the repository (commit history)—which work together to manage snapshots of your project. Most people mistakenly believe that Git saves files directly to a database when you run a command, but it actually requires a two-step process of staging changes before they are formally recorded as commits.

Git operates through a three-tier architecture to isolate uncommitted work from permanent history.

Area Primary Purpose Persistence Level
Working Directory Editing and viewing actual files Temporary/Untracked
Staging Area (Index) Preparing the next snapshot Intermediate/Pending
Git Repository (.git) Storing permanent history and metadata Permanent/Immutable

The working directory and staging area anatomy

Your working directory is the physical folder on your computer where you interact with your code. When you open a file in a text editor, change a line of code, and save that file, you are modifying the working directory. At this stage, Git is aware that a change has occurred because it constantly compares the current state of your files against the last known snapshot, but these changes are not yet part of the project's official history.

The staging area, often called the index, acts as a buffer between your messy, ongoing work and your clean, organized history. Think of the staging area as a loading dock. You might be moving many different boxes (files) around your warehouse (working directory), but you only place specific boxes onto the loading dock (staging area) when you are ready to ship them. This allows you to group related changes together. For example, if you fixed a bug in one file and started a new feature in another, you can stage only the bug fix, commit it, and then handle the feature separately. This granularity is what makes Git a powerful tool for professional development.

To move a file from the working directory to the staging area, you use the add command. This command tells Git to take the current state of that file and prepare it for the next snapshot. If you modify the file again after staging it, the version in the staging area remains the old version; you must run the add command again to update the index with the new changes. This distinction is a frequent source of confusion for new users who wonder why their latest edits did not appear in their most recent commit.

How the Git repository stores snapshots

Once you execute a commit command, the contents of the staging area are wrapped into a permanent snapshot and stored in the Git repository, located in the hidden .git directory. Unlike some older version control systems that store the differences between files (deltas), Git stores the entire state of the project as a series of snapshots. If a file has not changed between two commits, Git does not store the file again; instead, it simply creates a link to the previous version of that file to save space.

The anatomy of a single commit includes several critical pieces of metadata. First, it contains a unique identifier called a SHA-1 hash, which is a 40-character string of numbers and letters. This hash is generated based on the content of the files, the author information, the timestamp, and the hash of the parent commit. This creates a cryptographic chain where every commit is mathematically linked to the one before it. Second, it includes the author and committer information, which tells you who made the changes and when. Third, it includes a commit message, which provides a human-readable explanation of why the changes were made.

Under the hood, Git uses three main object types to build this repository: blobs, trees, and commits. A blob (binary large object) stores the actual content of a file but does not store the filename or permissions. A tree object acts like a directory; it maps filenames to blobs or other trees, effectively recreating the folder structure of your project. A commit object points to a specific tree (the root of the project at that moment) and includes the metadata mentioned above. This layered structure allows Git to navigate your project's history with extreme speed.

Anatomy of a Git workflow example

To understand how these parts interact in a real-world scenario, consider a developer working on a small web project. They need to update the header of a website and fix a typo in the footer.

  1. Modify the working directory: The developer opens index.html and changes the text in the header. They also open footer.html and fix the typo. At this point, the changes exist only in the working directory.
  2. Stage the header change: The developer runs the command to add index.html. Now, the staging area contains the new header, but the footer change remains only in the working directory.
  3. Commit the first part: The developer runs the commit command with a message like "Update website header". Git takes the staged index.html, creates a blob for it, creates a tree to represent the folder structure, and creates a commit object. The repository now has a permanent record of the header change.
  4. Stage the footer change: The developer runs the command to add footer.html. The staging area is updated with the fixed typo.
  5. Commit the second part: The developer runs the commit command for the footer. Git creates a new commit that points to the previous header commit as its parent, effectively building the chain of history.

By following this process, the developer has created two distinct, logical points in history rather than one giant, confusing commit that mixes unrelated tasks. This makes it much easier to revert a specific change later if the header update causes a layout issue but the footer fix is still desired.

Where Git anatomy causes errors

Errors in Git usually stem from a misunderstanding of which area a file currently resides in. One common mistake is forgetting that the staging area is a snapshot of the file at the moment you ran the add command. If you stage a file, then realize you made a small mistake and fix it in your editor, that fix is still only in your working directory. If you run commit immediately, the mistake will be included in the commit because the staging area was never updated with the correction.

Another frequent error involves the distinction between untracked files and modified files. An untracked file is a new file that has never been added to the Git repository. Git will see it in your working directory, but it will ignore it entirely during commits until you explicitly use the add command. If you accidentally delete an untracked file, Git cannot recover it because that file was never part of the repository anatomy. Only files that have been committed (and thus exist as blobs in the repository) can be restored using Git commands.

Finally, users often struggle with the concept of the HEAD pointer. In the anatomy of Git, HEAD is a special pointer that tells Git which branch and which commit you are currently working on. When you switch branches, Git moves the HEAD pointer to the tip of the new branch and updates your working directory to match that branch's state. If you perform a checkout or a reset and find your files have disappeared or changed unexpectedly, it is likely because the HEAD pointer moved to a different part of the commit history, causing the working directory to reconfigure itself to match that specific snapshot.

To verify the current state of your anatomy, you should regularly use the status command. This command provides a clear report of which files are modified in the working directory, which files are currently staged in the index, and which files are untracked. Checking the status before every commit is the best way to ensure that what you are about to record in the repository is exactly what you intended to save.