git stash
保存 uncommitted 更改 temporarily 使用 git stash — 切换上下文不使用 losing 工作.
你将学到
- 如何 stash and pop 更改到 切换上下文快速地
- Stashing 特定 files and 使用自定义消息
- Listing, applying, and dropping stashes
- difference 之间 stash pop and stash apply
概念讲解
What git stash Does
git stash saves your uncommitted changes (staged and unstaged) in a temporary stack, then restores your working directory to a clean state. This lets you switch branches or pull updates without committing half-finished work.
git stash
After stashing, git 状态 shows a clean working tree. Your changes are safely stored and can be retrieved later.
Popping Stashes
To retrieve your stashed changes, use git stash pop:
git stash pop
This applies the most recent stash and removes it from the stash stack. If there are conflicts, the stash is kept so you can resolve them and drop it manually.
Stash vs Apply
git stash apply # apply the stash but KEEP it in the stack
git stash pop # apply the stash and REMOVE it from the stack
Use apply if you want to apply the same stash to multiple branches. Use pop when you are done with the stash.
Stashing with a Message
By default, stashes are described as WIP on branchname. You can add a descriptive message:
git stash push -m "工作 in progress on 登录表单 validation"
This makes it much easier to find the right stash later when you have multiple stashed changes.
Listing and Managing Stashes
git stash list # see all stashes
git stash show -p # see what is in the latest stash
git stash show stash@{1} # see a specific stash
git stash drop # discard the latest stash
git stash drop stash@{1} # discard a specific stash
git stash clear # discard ALL stashes (dangerous!)
Stashes are referenced as stash@{0} (most recent), stash@{1} (second most recent), and so on.
Stashing Specific Files
# Stash only specific files
git stash push -m "布局更改" layout.css header.js
# Stash only staged changes
git stash --staged
# Stash including untracked files
git stash -u
The Typical Workflow
# You are working on a feature but need to switch context
git stash push -m "half-finished 登录 validation"
# Switch to main and fix an urgent bug
git switch main
git switch -c hotfix-urgent
# ... fix the bug, commit, merge ...
# Come back to your feature
git switch feature-login
git stash pop
# Your changes are back!
Stashing is the Git equivalent of "let me put 这个 aside for 时刻".
示例代码
# You have uncommitted changes but need to switch branches
echo "half-done work" >> login.js
git status # shows uncommitted changes
# Stash your work with a descriptive message
git stash push -m "login form validation in progress"
# Working tree is now clean — switch branches safely
git status # nothing to commit, working tree clean
git switch main
git pull
# Do something else (fix a bug, etc.)
git switch -c hotfix-bug
echo "fix" > fix.js
git add fix.js
git commit -m "fix: urgent bug"
git switch main
git merge hotfix-bug
# Come back and restore your stashed work
git switch feature-login
git stash list # see your stashes
git stash pop # restore the changes
# Stash only specific files
git stash push -m "only CSS" style.css
# Stash including untracked (new) files
git stash -u
# Apply a specific stash without removing it
git stash apply stash@{1}
# Drop a stash you no longer need
git stash drop stash@{0}
# See what is in a stash before applying
git stash show -p stash@{0}
Shows 完整 stash workflow: stashing 使用消息, 切换上下文, coming 后面 and popping, stashing 特定 files, stashing untracked files, applying 不使用 popping, and viewing stash 内容. descriptive 消息制作它 简单到 查找正确 stash later.
动手试一试
- Code Diff
Use git stash show -p 到 see stash 内容 as diff.
- .gitignore Generator
Keep 你的 workspace clean 所以你 stash 更少经常.
常见错误
错误写法
Using git stash 清晰 and losing stashed 工作 permanently
正确做法
git stash 清晰删除 ALL stashes 使用否 confirmation. 那里是 否简单撤销. 相反, use git stash drop stash@{n} 到移除特定 stashes one at 时间, and 总是验证使用 git stash 列表首先.
错误写法
Forgetting 关于 stash and leaving 工作 stranded for days
正确做法
总是 use git stash 推送 -m '描述' 所以你 能 identify stashes later. 运行 git stash 列表 periodically 到检查 for forgotten stashes. Stashes 是不 pushed 到 remotes, 所以它们仅 exist on 你的 machine — 做不 rely on 它们 for 长-term storage.
相关资源
相关速查表
- Git Cheatsheet
stash, stash pop, stash 列表, and stash drop commands.
练习 git stash
7 个练习
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。