States of a File in Git Working Directory

Git is a powerful version control system that helps developers track changes in their code over time. Understanding the different states a file can be in within the Git working directory is important for effectively managing and organizing your project. This article will explore the various states of a file in Git, explaining what each state means and how it impacts your workflow.

The Git Workflow

Before diving into the states of a file, it’s important to understand the basic Git workflow, which consists of three main areas:

  1. Working Directory: The current state of your project files.
  2. Staging Area (Index): A place where you can group changes before committing them.
  3. Repository (HEAD): The history of your project, including all committed changes.

States of Gile in Git Working Directory

1. Untracked state

Untracked files are any files in the working directory that were not in the last snapshot and are not in the staging area. Whenever a new file is added in the working directory which doesn’t exist before, it is considered as an untracked file. This is because Git sees this as a file that didn’t have in the previous snapshot(commit). Let’s see this with an example, suppose we add new file example.html in our repository and run git status command to see the status of the file. It shows a list of untracked files which include example.html file.

Untracked State in Git

2. Tracked state

Tracked files are files that were in the last snapshot. These are files that Git knows about. Each track file can be in one of three sub-states, modified, staged, or committed.

3. Modified State

A file in the modified state means that changes have been made to it that haven’t committed yet. The changes could be adding, modifying, or deleting the contents of the file. These files will be included in the next commit but will be included in their respective new form. Let’s modify our tracked example.html file and run the git status command.

Modified States in Git

4. Staging State

A file in the staging state means either it is not present in the last commit (e.g. newly created files) or it is “modified” file that user tells git to include in the next commit. Files are added to the staging state using git add command. Two types of files can be added to a staging state: untracked or modified. Let’s stage our untracked example.html file and run the git status command.

Staging State

Now, let’s stage our modified tracked example.html file and run the git status command.

Modified Tracked file

5. Committed State

A file in the committed state means that the changes made to it are safely stored in a snapshot in the Git directory. A file is committed using git commit command.This command creates a new snapshot in the Git directory and shows us some stats for the change made. Let’s commit the changes we made in our example.html file.

Git Commit