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:

git status provides all this information and serves as a "dashboard" for Git workflows.

(2) Basic Usage

BASH
# 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:


2. File Status

(1) Four File States

Files in Git have four main states:

100%
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:

100%
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

BASH
# 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:

TEXT
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:

Temporary Storage Area:

Non-buffered area:

Untracked files:

▶ Example: Analysis of Complete Status Output

BASH
# 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:

BASH
git status -s

The output format consists of two columns: tags and filenames:

TEXT
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

▶ Example: Comparing Detailed and Concise Output

BASH
# 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:

100%
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:

BASH
# 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

BASH
# 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

Q Why does git status show some files in red and others in green?
A The color indicates the file's status. Green means the file is staged and will be included in the next commit. Red means the file is not staged (either modified or untracked) and must be added with git add before it can be committed.
Q What does "working tree clean" mean?
A Indicates that the workspace is clean and contains no uncommitted changes. All changes have been committed to the repository, and there are no untracked, unstaged, or staged but uncommitted files. This is the ideal working state.
Q How do I view only staged files?
A Use 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.
Q What can I do if git status is running slowly?
A In large repositories, 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.
Q How can I exclude certain files from being displayed by git status?
A Create a file named .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


📝 Exercises

  1. Basic Exercise: Create a Git repository. Create a file, stage it, commit it, modify it, stage it again, and commit it again. Use git status to observe the changes in status at each step, and record the status transition process.

  2. Advanced Exercise: Create files in various states (untracked, modified, staged, partially staged), compare the detailed output of git status with the concise output of git status -s, and understand the meaning of the state markers.

  3. Challenge: Write a script to parse the output of git status -s and count the number of files in each state (untracked, staged, modified, etc.) for the purpose of automatically checking the repository status.

Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏