Checking Git Status and Managing File Status
git status is one of the most commonly used commands in Git. It helps you understand the status of your current working directory and see which files have been modified, which have been staged, and which are not being tracked.
1. Overview of git status
(1) Why is it necessary to check the status?
In a Git workflow, you need to always be aware of:
- Which files have been modified: What changes have been made to the workspace?
- Which files have been staged: Content ready to be committed
- Which files are not tracked: Newly created files that have not yet been managed
- Current Branch: Which branch are you working on?
- Relationship with Remote: Whether it is synchronized with Remote
git status provides all this information and serves as a "dashboard" for Git workflows.
(2) Basic Usage
# View Full Status
git status
# View Simplified View
git status -s
# Or
git status --short
(3) The Value of State Information
Status information helps you:
- Decide on the next step: Add, commit, or push
- Avoid leaving files out: Make sure all changes are committed
- Check Work Progress: Understand the current status of the work
- Detect Unintended Changes: Promptly identify files that should not be modified
2. File Status
(1) Four File States
Files in Git have four main states:
stateDiagram-v2
[*] --> Untracked: Create a New File
Untracked --> Staged: git add
Untracked --> Untracked: Edit
Staged --> Committed: git commit
Committed --> Modified: Edit File
Modified --> Staged: git add
Staged --> Modified: Edit the temporary file
Committed --> Committed: git commit
(2) Detailed Explanation of States
| Status | English Name | Description | Display Color | Recommended Action |
|---|---|---|---|---|
| Untracked | Untracked | New file, not tracked by Git | Red | Needs to be added with git add |
| Modified | Modified | Modified but not staged | Red | Needs to be git add |
| Staged | Staged | Added to the staging area | Green | Can be git committed |
| Committed | Committed | Committed to the repository | Not displayed | Clean |
(3) State Transition Process
Understanding state transitions is key to mastering Git:
graph TB
A[Create a New File] -->|Untracked| B[Not Tracked Status]
B -->|git add| C[Saved]
C -->|git commit| D[Submitted status]
D -->|Edit File| E[Status: Modified]
E -->|git add| C
C -->|Edit the temporary file| F[Partial Cache<br/>MMStatus]
F -->|git add| C
style B fill:#f8d7da
style C fill:#d4edda
style D fill:#c3e6cb
style E fill:#fff3cd
style F fill:#e2e3e5
▶ Example: Observing State Changes
# Initialize the repository
mkdir status-demo && cd status-demo
git init
# Create a New File
echo "# Demo Project" > README.md
echo "console.log('Hello');" > app.js
# View Status - Not tracked
git status
# On branch main
#
# No commits yet
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
# README.md
# app.js
#
# nothing added to commit but untracked files present
# Add a file to the staging area
git add README.md
# View Status - Partial Cache
git status
# On branch main
#
# No commits yet
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
# new file: README.md
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
# app.js
3. Detailed Explanation of Status Output
(1) Output Structure
The output of git status is divided into several parts:
On branch main ← Current branch
Your branch is up to date with 'origin/main'. ← Long-Distance Relationships
Changes to be committed: ← Saved Changes(Green)
(use "git restore --staged <file>..." to unstage)
modified: file1.txt
new file: file2.txt
Changes not staged for commit: ← Unsaved changes(Red)
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: file3.txt
Untracked files: ← Untracked files(Red)
(use "git add <file>..." to include in what will be committed)
file4.txt
no changes added to commit ← Summary Information
(2) Understand the meaning of each section
Branch Information:
On branch main: Currently on the main branchYour branch is ahead of 'origin/main' by 2 commits: Local is 2 commits ahead of remoteYour branch is behind 'origin/main' by 3 commits: The local repository is 3 commits behind the remote repository
Temporary Storage Area:
- These changes will be submitted during the next
git commitsession. - Displayed in green (on color-capable terminals)
- Includes new files, modified files, and deleted files
Non-buffered area:
- The file has been modified but not added to the staging area
- Displayed in red
- You must use
git addto submit
Untracked files:
- New files unknown to Git
- Displayed in red
- You must use
git addto manage it
▶ Example: Analysis of Complete Status Output
# Creating Complex Scenes
git init
echo "# Project" > README.md
git add README.md
git commit -m "Initial commit"
# Create files in various states
echo "new file" > new.txt # Not tracked
echo "change" >> README.md # Edited but not saved
echo "staged" > staged.txt
git add staged.txt # New files that have been cached
echo "more" >> staged.txt # Saved and then modified
# View Full Status
git status
# On branch main
# Changes to be committed:
# new file: staged.txt
#
# Changes not staged for commit:
# modified: README.md
# modified: staged.txt
#
# Untracked files:
# new.txt
4. Status Overview
(1) Concise Output Format
Use the -s or --short options to get a concise output:
git status -s
The output format consists of two columns: tags and filenames:
XY filename
(2) Meaning of Status Indicators
| Label | Column X (Temporary Area) | Column Y (Work Area) | Description |
|---|---|---|---|
?? |
- | - | Untracked files |
A |
Added | - | Newly added to staging |
M |
Modified | - | Modified and saved as draft |
M |
- | Modified | Modified but not saved |
MM |
Modified | Modified | Staged then Modified |
D |
Deleted | - | Deleted and saved to draft |
D |
- | Deleted | Deleted but not saved to draft |
AD |
Added | Deleted | Deleted from the workspace after being added |
R |
Renamed | - | Renamed and saved as a draft |
C |
Copied | - | Copied and saved temporarily |
(3) The Advantages of Concise Output
- Quick View: See all statuses at a glance
- Script-friendly: Easy to parse and process
- Space-saving: Compact design, ideal for end-user devices
- Color Coding: Keep color indicators (green for cached, red for not cached)
▶ Example: Comparing Detailed and Concise Output
# Create Multiple States
echo "untracked" > file1.txt
echo "content" > file2.txt
git add file2.txt
echo "more" >> file2.txt
echo "tracked" > file3.txt
git add file3.txt
git commit -m "Add files"
echo "modified" >> file3.txt
# Detailed Output
git status
# On branch main
# Changes to be committed:
# new file: file2.txt
#
# Changes not staged for commit:
# modified: file3.txt
#
# Untracked files:
# file1.txt
# Concise Output
git status -s
# A file2.txt
# M file3.txt
# ?? file1.txt
5. Tracking State Changes
(1) Typical Workflow State Changes
Let's walk through a complete Git workflow:
sequenceDiagram
participant W as Workspace
participant S as Buffer
participant R as Repository
Note over W: 1. Create a New File
W->>W: Status: Untracked
Note over W,S: 2. git add
W->>S: Move to the staging area
S->>S: Status: Staged
Note over S,R: 3. git commit
S->>R: Commit to the repository
R->>R: Status: Committed
Note over W: 4. Edit File
W->>W: Status: Modified
Note over W,S: 5. git add
W->>S: Update the temporary storage area
S->>S: Status: Staged
Note over S,R: 6. git commit
S->>R: Submit Changes
R->>R: Status: Committed
(2) Special Status: Partially Cached
When a file has been staged but new changes have been made in the workspace, a special status occurs:
# Create and stage a file
echo "version 1" > file.txt
git add file.txt
git status -s
# A file.txt
# Continue editing the file
echo "version 2" >> file.txt
git status -s
# AM file.txt
# AIndicates that it has been saved temporarily(version 1)
# MIndicates that there are new changes in the workspace(version 2)
# Add Again
git add file.txt
git status -s
# A file.txt
# The temporary storage area is currentlyversion 2
(3) The Relationship Between State and Git Commands
| Current Status | Available Commands | Result |
|---|---|---|
| Untracked | git add |
Becomes Staged |
| Untracked | git clean |
Delete File |
| Modified | git add |
Becomes Staged |
| Modified | git restore |
Reverted the change; status changed to Committed |
| Staged | git commit |
Becomes Committed |
| Staged | git restore --staged |
Unstage, change to Modified or Untracked |
| Committed | Edit File | Marked as Modified |
▶ Example: Complete Workflow Tracking
# Initialization
git init status-workflow
cd status-workflow
# Steps1:Create a File(Untracked)
echo "# Project" > README.md
git status -s
# ?? README.md
# Steps2:Add to Stash(Staged)
git add README.md
git status -s
# A README.md
# Steps3:Submit(Committed)
git commit -m "Add README"
git status
# nothing to commit, working tree clean
# Steps4:Edit File(Modified)
echo "## Features" >> README.md
git status -s
# M README.md
# Steps5:Save Changes Temporarily(Staged)
git add README.md
git status -s
# M README.md
# Steps6:Revise Again(Partial Cache)
echo "## Usage" >> README.md
git status -s
# MM README.md
# Steps7:Commit Staged Changes
git commit -m "Add features"
git status -s
# M README.md (There are still changes to be made in the workspace.)
# Steps8:Submit the remaining changes
git add README.md
git commit -m "Add usage"
git status
# nothing to commit, working tree clean
❓ FAQ
git status show some files in red and others in green?git add before it can be committed.git diff --staged or git diff --cached to view the changes you've staged. If you want to view the file list, use git status -s to see only the files marked on the left.git status is running slowly?git status may run slowly due to the large number of files. You can use git status -s for a concise output, or configure git config core.ignoreStat true to skip file status checks. You can also use git status --untracked-files=no to ignore untracked files.git status?.gitignore in the project's root directory and list the file patterns to be ignored. For example, *.log ignores all log files, and node_modules/ ignores dependency directories. Ignored files will not appear in git status.📖 Summary
git statusis the most commonly used Git command, which helps you understand the status of your current working directory- Files have four states: Untracked, Modified, Staged, and Committed
- Status output is divided into three categories: staged, unstaged, and untracked; green indicates staged items and red indicates unstaged items.
- Concise output
git status -suses two columns to indicate status, making it easy to scan and suitable for script processing - In the XY status tags, the X column represents the status of the temporary area, and the Y column represents the status of the work area.
- Understanding state transitions is key to mastering the Git workflow; each state corresponds to different available commands.
- Partially staged (MM status) indicates that the file has been staged but there are new changes in the working directory.
📝 Exercises
-
Basic Exercise: Create a Git repository. Create a file, stage it, commit it, modify it, stage it again, and commit it again. Use
git statusto observe the changes in status at each step, and record the status transition process. -
Advanced Exercise: Create files in various states (untracked, modified, staged, partially staged), compare the detailed output of
git statuswith the concise output ofgit status -s, and understand the meaning of the state markers. -
Challenge: Write a script to parse the output of
git status -sand count the number of files in each state (untracked, staged, modified, etc.) for the purpose of automatically checking the repository status.



