Git Status and File States

Updated · 11 min read
easyGit 2.53gitgit-statusfile-statesuntrackedinterview

A driver glances at the dashboard many times on every trip. Speed, fuel, warning lights: one look, and they know the state of the car before they do anything else.

git status is Git's dashboard. It tells you which branch you are on, which files have changed, which changes are staged, and often which command to run next. It changes nothing, so you can run it as often as you like. Experienced developers run it before and after almost every other command.

In this lesson, you will learn the four states a file can be in, read the output of git status line by line, and use its short format.

The Four File States

Every file in your project folder is in one of four states.

The four states of a file and what moves a file between them
StateMeaningTracked?
UntrackedA new file that has never been added. Git sees it and leaves it alone.No
UnmodifiedThe file is the same as in the last commit.Yes
ModifiedThe file has changed since the last commit, and the change is not staged.Yes
StagedThe change has been added and will be in the next commit.Yes

A tracked file is one that Git knows about: it was in the last commit, or it has been staged. Everything else is untracked.

The states form a circle. You edit a file and it becomes modified. You add it and it becomes staged. You commit and it is unmodified again, ready for the next edit.

A Clean Working Tree

Navika's Fee Tracker repository has one commit with two files in it:

Terminal
mkdir fee-tracker
cd fee-tracker
git init --quiet
echo "Fee Tracker" > README.md
echo 'Term 1 fee: $12,000' > fees.txt
git add README.md fees.txt
git commit --quiet -m "Add README and fee list"
git status
Output
On branch main
nothing to commit, working tree clean

This is the answer you want to see before you start something new. "Working tree clean" means every tracked file is unmodified and there are no untracked files. Unmodified files are never listed, because there is nothing to say about them.

One File in Each State

Now Navika makes three changes: she stages a new Java file, edits fees.txt, and creates a notes file.

Terminal
echo "class FeeCalculator {}" > FeeCalculator.java
git add FeeCalculator.java
echo 'Term 2 fee: $12,500' >> fees.txt
echo "Ideas for later" > notes.txt
git status
Output
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	new file:   FeeCalculator.java
 
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   fees.txt
 
Untracked files:
  (use "git add <file>..." to include in what will be committed)
	notes.txt

The repository now has a file in every state: FeeCalculator.java is staged, fees.txt is modified, notes.txt is untracked, and README.md is unmodified, so it isn't listed.

Read the Output Line by Line

The output always has the same shape: one line about the branch, then up to three sections.

Part of the outputWhat it tells you
On branch mainThe branch you are working on
Changes to be committed:The staging area: what the next commit will contain
new file: FeeCalculator.javaThis file is staged, and it is new to the repository
Changes not staged for commit:Tracked files that are modified in the working tree
modified: fees.txtThis file differs from the last commit, and the change is not staged
Untracked files:Files Git is not tracking
Lines in parenthesesGit's suggestion for what you can do next

The lines in parentheses are worth reading. They name the command for each direction you might want to go:

The hint saysIt means
use "git add <file>..." to update what will be committedStage this change
use "git add <file>..." to include in what will be committedStart tracking this new file
use "git restore --staged <file>..." to unstageTake the change out of the staging area and keep it in the file
use "git restore <file>..." to discard changes in working directoryThrow the change away and return the file to its last committed version

The Short Format

Once you can read the long output, you will often prefer the short one. Add -s (or --short):

Terminal
git status -s
Output
A  FeeCalculator.java
 M fees.txt
?? notes.txt

Each file gets one line with two letters in front. The position of a letter matters:

  • The left column is the staging area.
  • The right column is the working tree.
CodeMeaning
??Untracked
A (left)New file, staged
M (left)Modified, and the change is staged
M (right)Modified, not staged
MMStaged, then modified again
D (right)Deleted from the folder, and the deletion is not staged
D (left)Deletion staged

So A FeeCalculator.java has its letter on the left: staged. M fees.txt has its letter on the right: changed in the working tree only.

Watch what happens when fees.txt is staged and then edited once more:

Terminal
git add fees.txt
echo 'Term 3 fee: $13,000' >> fees.txt
git status -s
Output
A  FeeCalculator.java
MM fees.txt
?? notes.txt

MM means there is a staged change (left) and a newer, unstaged change (right) in the same file. This is the "edit after add" situation from the lesson "Working Tree, Staging Area and Repository", and the short format makes it easy to spot.

Add -b to include the branch line as well:

Terminal
git status -sb
Output
## main
A  FeeCalculator.java
MM fees.txt
?? notes.txt

What git status Does Not Show

Three things are missing from the output on purpose.

Unmodified files. To list every tracked file, changed or not, use git ls-files:

Terminal
git ls-files
Output
FeeCalculator.java
README.md
fees.txt

notes.txt is not in the list, because it is untracked.

Empty folders. Git tracks files, not folders. A folder with nothing in it doesn't exist as far as Git is concerned:

Terminal
mkdir reports
git status -s
Output
A  FeeCalculator.java
MM fees.txt
?? notes.txt

The new reports folder is not mentioned. It will appear as soon as it contains a file.

Ignored files. You can tell Git to ignore files that should never be committed, such as compiled .class files. They don't appear in git status at all. The lesson on .gitignore, later in the course, shows how.

Common Mistakes

Not running it. Most beginner accidents, such as a commit with the wrong files or missing changes, would have been visible in git status a moment earlier.

Reading "untracked" as an error. It only means the file is new to Git. Add it if it belongs to the project, and leave it or ignore it if it doesn't.

Expecting an empty folder to be committed. Git doesn't record empty folders. If a project needs one, the custom is to put a small placeholder file inside it, often named .gitkeep.

Mixing up the two columns of the short format. M and M look alike and mean opposite things for your next commit. Left is staged.

Thinking "nothing to commit" means everything is shared. git status talks about your working tree and staging area. A clean status says nothing about whether your commits have reached GitHub.

Interview Corner

What are the states of a file in Git?

A file is either untracked or tracked. A tracked file is unmodified (same as the last commit), modified (changed in the working tree but not staged) or staged (the change is in the index and will be in the next commit). Committing turns staged files back into unmodified ones.

What is the difference between a tracked and an untracked file?

A tracked file is one Git already knows: it was in the last commit or has been added to the staging area. An untracked file exists in the working tree but has never been added. Git reports untracked files and never includes them in a commit until you run git add.

What does git status show?

The current branch, the changes staged for the next commit, the changes in tracked files that are not staged, and the untracked files. It also suggests the commands for staging, unstaging and discarding. It does not change anything in the repository.

In git status -s, what is the difference between M and M?

The first column describes the staging area and the second the working tree. M (letter on the left) means the modification is staged. M (letter on the right) means the file is modified but not staged. MM means both: a staged change plus a later unstaged one.

Why doesn't Git show my empty folder?

Git tracks file content and stores folders only as paths to files. A folder with no tracked files has nothing to record. The usual workaround is a placeholder file such as .gitkeep.

Practice

Challenge: predict the short status. Continue in the same repository. Stage notes.txt. Then add the line Tracks school fee payments. to README.md and do not stage it. Print the short status.

What does Git print? Before you open the solution, write down the two-letter code for each of the four files. The current codes are in the last output above.

Show solution
Terminal
git add notes.txt
echo "Tracks school fee payments." >> README.md
git status -s
Output
A  FeeCalculator.java
 M README.md
MM fees.txt
A  notes.txt

notes.txt moved from ?? to A : it is now a new, staged file. README.md was unmodified and unlisted, and now shows M with the letter on the right, because the edit is not staged. The other two files did not change state.

Checkpoint. In a practice repository of your own, make one file untracked, one modified, one staged and one unmodified at the same time. Run git status and explain every line of the output aloud, including the hints in parentheses.

Quick check: Git status

4 questions
1What does "nothing to commit, working tree clean" mean?
2A file is listed under "Untracked files". What is true?
3In git status -s, a line reads M fees.txt with the letter in the right column. What does it mean?
4You create an empty folder named reports. What does git status show for it?

Key Takeaways

Quick reference

CommandIt is used to
git statusShow the state of the working tree and the staging area
git status -sShow the same in short form, one line for each file
git status -sbShort form with the branch line
git ls-filesList every tracked file
Short codeMeaning
??Untracked
A New file, staged
M Modified, staged
MModified, not staged
MMStaged, then modified again

Was this tutorial helpful?

Lesson 10 of 106