A Detailed Explanation of Git Staging and Multitasking
Stashing is a feature that temporarily saves changes made to your workspace. When you need to switch branches to handle an urgent task but don’t want to commit unfinished work, stashing is the best option. Understanding how to use stashing is crucial for multitasking in development.
1. Basic Concepts of Temporary Storage
(1) What Is a Temporary Storage Area?
Staging temporarily saves changes made to the workspace and staging area:
- Temporary Save: Save unsubmitted changes
- Clean Up the Workspace: Restore the workspace to a clean state
- Switch Branches: You can freely switch between branches
- Resume Later: Resume your saved changes at any time
graph TB
A[Changes have been made to the workspace] --> B[git stash]
B --> C[Edit and save tostash]
C --> D[The workspace is now clean]
D --> E[Switch Branches]
E --> F[git stash pop]
F --> G[Restore Changes]
style B fill:#fff3cd
style D fill:#d4edda
style G fill:#c3e6cb
(2) The Difference Between "stash" and "commit"
| Feature | stash | commit |
|---|---|---|
| Purpose | Temporary Storage | Permanent Record |
| History | Not in commit history | In commit history |
| Branch | Not on any branch | On the current branch |
| Push Notifications | No push notifications | Push notifications |
| Use Cases | Temporary task switching | Completing feature development |
(3) The storage structure of stash
A stash is a stack:
- stash@{0}: The latest stash
- stash@{1}: Recently added items in the stash
- stash@{n}: The n+1st stash
2. Basic Temporary Storage Operations
(1) Save the current changes temporarily
▶ Example: Staging Changes
# View Current Changes
git status
# Output:
# Changes not staged for commit:
# modified: src/auth.js
# modified: src/user.js
# Save current changes temporarily
git stash
# Output:
# Saved working directory and index state WIP on main: a1b2c3d feat: Add Feature
# View Status(The workspace is clean)
git status
# Output:
# On branch main
# nothing to commit, working tree clean
(2) Staging with a description
▶ Example: Add a description
# Save for now and add a description
git stash save "WIP: User Login Feature"
# Or usepush(New Grammar)
git stash push -m "WIP: User Login Feature"
# Output:
# Saved working directory and index state On main: WIP: User Login Feature
# View the staging list
git stash list
# Output:
# stash@{0}: On main: WIP: User Login Feature
(3) Staging includes untracked files
▶ Example: Includes untracked files
# Create a New File(Not tracked)
touch new-file.js
# DefaultstashUntracked files will not be saved
git stash
# Output:
# Saved working directory and index state WIP on main: a1b2c3d
# new-file.js still untracked
# Usage-uThe selection includes untracked files
git stash -u
# Or
git stash --include-untracked
# Usage-aThe option includes all files(Include ignored files)
git stash -a
3. Viewing and Managing Staging
(1) View the staging list
▶ Example: List all staging areas
# View the staging list
git stash list
# Output:
# stash@{0}: On main: WIP: User Login Feature
# stash@{1}: On feature: WIP: Shopping Cart Feature
# stash@{2}: On develop: FixBug
# Limit the number of items displayed
git stash list -3
# View Staging Details
git stash show
# Output:
# src/auth.js | 10 ++++++++++
# src/user.js | 5 ++---
# 2 files changed, 12 insertions(+), 3 deletions(-)
# View Detailed Differences
git stash show -p
# View a Specific Cache
git stash show stash@{1}
(2) View Staged Content
▶ Example: Viewing Staging Details
# View the latest full diff from the staging area
git stash show -p stash@{0}
# Output:
# diff --git a/src/auth.js b/src/auth.js
# index abc1234..def5678 100644
# --- a/src/auth.js
# +++ b/src/auth.js
# @@ -10,6 +10,16 @@ function validate() {
# + // Added validation logic
# + if (!token) {
# + return false;
# + }
# return true;
# }
# View Cached Statistics
git stash show --stat
# Output:
# src/auth.js | 10 ++++++++++
# src/user.js | 5 ++---
# 2 files changed, 12 insertions(+), 3 deletions(-)
(3) Delete the staging area
▶ Example: Deleting a staging area
# Delete the latest staging
git stash drop
# Delete Specified Stash
git stash drop stash@{2}
# Clear All Cache
git stash clear
# Confirm Deletion
git stash list
4. Application Staging
(1) Apply and pop the staging area
▶ Example: Application Staging
# Apply, Save, and Delete
git stash pop
# Output:
# On branch main
# Changes not staged for commit:
# modified: src/auth.js
# modified: src/user.js
# Use Specified Stash
git stash pop stash@{1}
# If there is a conflict
# CONFLICT (content): Merge conflict in src/auth.js
# The stash entry is kept in case you need it again.
# After Resolving the Conflict,Manually Delete the Stash
git stash drop
(2) Apply but keep in staging (apply)
▶ Example: Keep in Stash
# App is cached but not deleted
git stash apply
# Use Specified Stash
git stash apply stash@{1}
# View the staging list(The temporary file is still there)
git stash list
# Output:
# stash@{0}: On main: WIP: User Login Feature
# stash@{1}: On feature: WIP: Shopping Cart Feature
# applySuitable for multiple uses of the same temporary storage
(3) pop vs apply
graph TB
A[App Stash] --> B{Method}
B -->|pop| C[Apply and Clear the Clipboard]
B -->|apply| D[Apply but keep in the staging area]
C --> E[Single-use]
D --> F[Reusable]
style C fill:#d4edda
style D fill:#fff3cd
Scenarios for using pop:
- Apply only once
- Confirm that this staging area is no longer needed
When to use apply:
- Multiple applications may be necessary
- Want to keep the staging area as a backup
- Apply to multiple branches
5. Detailed Explanation of Temporary Storage Options
(1) Common Temporary Storage Options
| Option | Description | Use Case |
|---|---|---|
save |
Save to Clipboard (Old Syntax) | Basic Clipboard |
push |
Save to Clipboard (New Syntax) | Recommended |
-m |
Add description | Mark as draft |
-u |
Includes untracked files | New files must also be staged |
-a |
Include all files | Include ignored files |
-k |
Keep staging area | Stage workspace only |
▶ Example: Using the "Temporary Storage" option
# Only cache changes to tracked files
git stash
# Check-in includes untracked files
git stash -u
# Check out includes ignored files
git stash -a
# Temporary Workspace Only,Reserve a buffer
git stash -k
# Cache only specific files
git stash push src/auth.js
# Cache only files that match the pattern
git stash push '*.js'
# Preserve the temporary storage index
git stash --index
(2) Creating a Branch from a Stash
▶ Example: Creating a branch from a staging area
# Create a New Branch from a Stash
git stash branch feature-from-stash
# Output:
# Switched to a new branch 'feature-from-stash'
# On branch feature-from-stash
# Changes not staged for commit:
# modified: src/auth.js
# This will create a new branch and apply the staged changes.
# Ideal for transferring staged changes to a new branch for development
6. Save Workflow
(1) Emergency Task Switching
▶ Example: Handling an Urgent Bug
# Features currently under developmentA
echo "feature A code" > feature-a.js
git status
# Output:
# Untracked files:
# feature-a.js
# Sudden need for urgent repairsBug
# Save the current work
git stash save "WIP: FeaturesAUnder development"
# Switch tohotfixBranch
git checkout -b hotfix-urgent-bug
# FixBug
echo "fix" > fix.js
git add fix.js
git commit -m "fix: Urgent FixBug"
# Push Fix
git push origin hotfix-urgent-bug
# Switch back to the original branch and resume work
git checkout main
git stash pop
# Continue Developing FeaturesA
(2) Parallel Development of Multiple Tasks
sequenceDiagram
participant Developer
participant mainBranch
participant featureBranch
participant Stash
Developer->>mainBranch: Development FeaturesA
Developer->>Stash: git stash Save A
Developer->>featureBranch: Switch to Branch DevelopmentB
Developer->>Stash: git stash Save B
Developer->>mainBranch: Cut back tomain
Developer->>Stash: git stash pop Restore A
▶ Example: Switching Between Tasks
# Development FeaturesA
echo "feature A" > feature-a.js
git stash push -m "FeaturesA"
# Toggle Development FeaturesB
git checkout -b feature-b
echo "feature B" > feature-b.js
git stash push -m "FeaturesB"
# View the staging list
git stash list
# Output:
# stash@{0}: On feature-b: FeaturesB
# stash@{1}: On main: FeaturesA
# Back tomainContinue DevelopmentA
git checkout main
git stash pop stash@{1}
# Completed FeaturesA
git add feature-a.js
git commit -m "feat: FeaturesADone"
# Cut tofeature-bContinue DevelopmentB
git checkout feature-b
git stash pop
(3) Staging the application to a different branch
▶ Example: Staging Changes Across Branches
# Staging Changes on feature Branch
git checkout feature
echo "some changes" > file.js
git stash save "Share Edits"
# Switch tomainBranch Application Staging
git checkout main
git stash apply
# NowmainThese changes are also present in the branch.
# The temporary file is still there,Can be applied to other branches
# Switch todevelopBranch Applications
git checkout develop
git stash apply
# Delete Stash
git stash drop
7. Best Practices for Temporary Storage
(1) Recommendations for Temporary Storage
Best Practices:
# Add a clear description
git stash save "WIP: User Authentication Feature,Complete the login logic"
# Clear the cache list periodically
git stash list
git stash drop stash@{5} # Delete the old staging area
# Usageapplyrather thanpop,If you're not sure whether you still need it
git stash apply
# Check the status before saving temporarily
git status
git diff
Bad Practices:
# No description,Difficult to identify
git stash
# Retaining large amounts of temporary data over the long term
git stash list # Dozens of temporary files
# Forgot to restore the staging area
git stash
# ... A few days later, I forgot what I had saved.
(2) Handling Conflicts in the Staging Area
▶ Example: Resolving Staging Conflicts
# Conflicts caused by app cache
git stash pop
# Output:
# CONFLICT (content): Merge conflict in src/auth.js
# The stash entry is kept in case you need it again.
# View Conflicting Files
git status
# Resolving Conflicts
# Editing Files with Conflicts,Keep the necessary content
# Mark the conflict as resolved
git add src/auth.js
# Delete Stash
git stash drop
# Or discard the temporary file
git reset --hard
git stash drop
(3) Temporary Storage Management Script
▶ Example: Staging Management Tool
#!/bin/bash
# stash-manager.sh - Cache Management Tool
case "$1" in
list)
echo "=== Stash List ==="
git stash list
;;
show)
if [ -z "$2" ]; then
git stash show -p
else
git stash show -p "stash@{$2}"
fi
;;
apply)
if [ -z "$2" ]; then
git stash apply
else
git stash apply "stash@{$2}"
fi
;;
drop)
if [ -z "$2" ]; then
git stash drop
else
git stash drop "stash@{$2}"
fi
;;
clear)
read -p "Clear all stashes? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
git stash clear
echo "All stashes cleared"
fi
;;
*)
echo "Usage: $0 {list|show [n]|apply [n]|drop [n]|clear}"
;;
esac
❓ FAQ
stash pop and stash apply?pop applies the commit and deletes the commit record, while apply applies the commit but retains the commit record. If you need to apply the same commit multiple times, use apply.git stash -u or git stash --include-untracked to include untracked files. Use git stash -a to include all files (including ignored files).git stash list to view the staging list, git stash show to view staging statistics, and git stash show -p to view the complete differences in the staging list.git add to mark the conflict as resolved, then use git stash drop to remove the changes from staging. If you want to discard the changes, use git reset --hard and git stash drop.📖 Summary
- "Save to Clipboard" is a feature that temporarily saves changes made to the workspace, allowing you to switch between tasks.
- Basic operations:
git stashSave to clipboard,git stash popApply and delete - With description:
git stash save "description"orgit stash push -m "description" - Include untracked:
-uThis option includes untracked files - View Stash:
git stash listList,git stash showDetails - pop vs. apply: pop removes the temporary value, while apply preserves it
- Staging the workflow: Stage the current work → Switch branches → Complete the task → Unstage
📝 Exercises
-
Basic Exercise: After modifying a file, use
stashto stash the changes, view the stash list and details, and then apply the stash to experience the complete stashing process. -
Advanced Exercise: Simulate a scenario where you need to switch tasks urgently: While developing a feature, stash your current work, switch branches to fix a bug, then restore the stash and continue development to experience the practical application of stashing.
-
Challenge: Experience parallel development of multiple tasks: Create multiple staging areas, switch between different branches to apply different staging areas, and understand how the staging stack works.



