git stash 是一个用于临时保存("stashing")工作目录中的修改的 Git 命令。这在需要临时切换到其他分支,但不想提交当前工作的修改时非常有用。
基础用法
-
保存修改: 使用
git stash或git stash save将暂存区和工作目录中的修改保存到一个新的储藏。git stash -
查看储藏列表: 使用
git stash list查看所有的储藏。git stash list -
应用储藏: 使用
git stash apply应用最近的储藏。git stash apply对于特定的储藏,使用:
git stash apply stash@{n} -
删除储藏: 使用
git stash drop删除最近的储藏。git stash drop对于特定的储藏,使用:
git stash drop stash@{n} -
弹出储藏: 使用
git stash pop应用最近的储藏,并删除它。git stash pop
高级用法
-
保存未跟踪的文件: 使用
git stash -u或git stash --include-untracked将未跟踪的文件也保存在储藏中。git stash -u -
应用并保留储藏: 默认情况下,
git stash apply会保留储藏。但如果你想在应用之后立即删除它,可以使用git stash pop。 -
命名储藏: 你也可以给储藏一个描述性的名字。
git stash save "Your stash message here"
这只是 git stash 的一些基本和高级用法,但它提供了一种灵活的方式来管理工作目录的修改。