Git tracks files by using a staging area called the index, which acts as a middle ground between your working directory and your permanent commit history. For example, when you run git add file.txt, Git takes a snapshot of that file and places a reference to it in the index, marking it as ready for the next commit.

How the Git Index Tracks Changes

Git does not track changes to files line-by-line in the way some older version control systems do. Instead, it tracks content. When you modify a file, Git sees that the content has changed and marks the file as modified. To move this change into the tracking system, you use the index. The index is a binary file located in your .git/index folder that lists every file in your project and the specific version (blob) of that file that should be included in the next snapshot.

When you execute a command to add a file, Git performs three distinct actions. First, it creates a blob object in the .git/objects directory containing the current content of the file. Second, it calculates a SHA-1 hash based on that content to give the blob a unique ID. Third, it updates the index file to map the file path (e.g., src/main.js) to that specific SHA-1 hash. This means the index is essentially a manifest of the next commit.

This architecture allows you to be selective about what you commit. You can change ten different files in your working directory but only add two of them to the index. When you finally commit, Git does not look at your working directory at all; it simply wraps the current state of the index into a tree object and creates a commit pointing to that tree. This separation ensures that your commits are intentional and clean, rather than just a dump of everything you happened to be editing at the time.

The Three States of Git Tracking

To understand the anatomy of tracking, you must distinguish between the three main areas where your code lives. The first is the Working Directory, which is the actual folder on your hard drive where you open files in your editor. Files here can be untracked, modified, or staged. An untracked file is one that Git knows exists in the folder but has no record of in the index.

The second area is the Staging Area, also known as the Index. This is the draft of your next commit. When a file is staged, it is tracked and its current version is locked into the index. If you modify the file again after staging it, the version in the index remains the same as it was when you ran the add command, while the version in the working directory continues to change. This creates a state where a file is both staged and modified simultaneously.

The third area is the Git Directory, or the Repository. This is where Git stores the compressed snapshots and the history of your project. Once you commit, the state of the index is permanently recorded as a commit object. The index is then cleared of its "staged" status, but it still tracks the files at the version they were just committed. This cycle of Working Directory → Index → Repository is the fundamental heartbeat of Git tracking.

Why Files Become Untracked or Ignored

A file is untracked when it exists in the working directory but is not listed in the index. This happens with every new file you create. Git will notify you of these files during a status check, suggesting that you add them. However, there are many files you never want Git to track, such as local configuration files, log files, or large dependency folders like node_modules. This is where the .gitignore file comes into play.

The .gitignore file tells Git to intentionally ignore specific patterns. When Git scans your working directory to see what has changed, it checks the ignore rules first. If a file matches a pattern in .gitignore, Git treats it as if it does not exist, and it will not appear as an untracked file. This prevents the index from becoming cluttered with temporary files that would only slow down the repository and create noise in the history.

A common point of confusion occurs when a file is already being tracked in the index, but you later add it to .gitignore. Git will continue to track the file because the ignore rule only applies to untracked files. To stop tracking a file that is already in the index without deleting it from your hard drive, you must explicitly remove it from the index using a command that removes the file from the staging area while keeping the physical file intact. Only after the file is removed from the index will the .gitignore rule take effect.

What Happens During a Merge Conflict

Merge conflicts are a direct result of how Git tracks content in the index. When you merge two branches, Git attempts to combine the snapshots. If both branches modified the same line of the same file, Git cannot automatically decide which version is correct. In this scenario, Git expands the index to hold multiple versions of the same file.

In a normal state, the index has one entry per file path. During a conflict, the index stores three versions: the common ancestor (the last version both branches shared), the version from your current branch, and the version from the branch you are merging. This is why you see conflict markers like <<<<<<< and ======= in your editor. Git is literally showing you the different versions it is tracking in the index for that specific file.

To resolve the conflict, you must edit the file to the desired final state and then add it back to the index. Running the add command tells Git, "I have resolved the differences; please replace those three conflicting versions in the index with this single, final version." Once the index is back to having one version per file, the conflict is marked as resolved, and you can complete the commit.

Tracking Failures and Index Corruption

While rare, the index can become corrupted or out of sync with the actual filesystem. This often happens during hard system crashes or when using external drives that disconnect unexpectedly. Signs of index issues include git status taking an unusually long time to run, or Git reporting that a file is modified when you know it hasn't been touched. This usually happens because the file's metadata (like the timestamp or size) in the index does not match the actual file on disk.

If the index becomes corrupted, the safest fix is to remove the index file itself. Because the index is just a manifest of the next commit and not the actual history of the project, deleting the .git/index file does not lose any of your committed work. After deleting the index, you can rebuild it by adding all the files in your working directory back to the staging area. Git will scan the files, re-calculate the hashes, and create a fresh, accurate index.

Another common tracking error is the "case-sensitivity trap." On Windows and macOS, the filesystem is typically case-insensitive (it treats File.txt and file.txt as the same). However, Git is case-sensitive. If you rename a file from User.js to user.js, the operating system might not tell Git that a change occurred. In these cases, you must use a specific Git command to rename the file in the index, rather than relying on the OS file explorer, to ensure the tracking remains accurate across different platforms.

Git Tracking Reference Table

State Location Git's Knowledge Action to Move
Untracked Working Directory Exists, but not in index git add
Modified Working Directory Differs from index version git add
Staged Index Ready for next commit git commit
Committed Git Directory Permanent snapshot git checkout

A useful rule of thumb for tracking is: The index is a preview of the next commit. If you aren't sure what will be in your next snapshot, check the index, not your folder. Whenever you are confused about why a file is behaving strangely, remember that Git is tracking the content hash, not the filename or the date. If the content is the same, the hash is the same, and Git considers the file unchanged, regardless of what the operating system says about the file's metadata.

Git Tracking for Binary Files and Large Assets

Tracking text files is straightforward because Git can calculate diffs between versions. However, tracking binary files (like images, PDFs, or compiled binaries) is different. Since binary files cannot be merged line-by-line, Git tracks them as entire blobs. Every time you change one pixel in a large image and commit it, Git stores a nearly complete new copy of that image in the .git/objects folder.

This can lead to repository bloat, where the .git folder becomes larger than the actual project. For these types of files, standard tracking is often inefficient. You can check the size of your objects folder to see if binary tracking is causing issues. If the folder size grows rapidly despite few changes to the code, you may be tracking too many large assets.

To handle this, you can use specific attributes to tell Git how to treat certain files. By creating a .gitattributes file, you can mark files as binary, which tells Git not to attempt to merge them or show text-based diffs. While this doesn't stop the storage growth, it prevents the index from trying to "fix" line endings in binary data, which would corrupt the files. For truly massive files, you would typically move them out of the standard Git tracking system and into a dedicated large-file storage system that replaces the file in the index with a small text pointer.