首页·终端命令

git stash

git

用法

git stash [push] [-u] [-m "<message>"]

把没做完的改动先收起来,让工作区变干净;不加 -u 时未跟踪文件会留在原地,pop 取出后会删掉那条记录,apply 则保留。

常用选项

选项含义
-uinclude untracked files, which are otherwise left behind
-ainclude ignored files too
-kkeep the staging area as it is
-pstash selected hunks
popapply the newest entry and delete it
applyapply it and keep the entry
listshow the entries
drop <stash>delete one entry

示例

git stash -u -m "wip login"

parks tracked and untracked changes under a name

git stash pop

brings the newest stash back and removes it from the list

git stash apply stash@{2}

applies an older entry and leaves it in the list

难的地方在于撤销有好几种:把分支的指向挪走,和补一个反向提交,不是一回事。

怎么看

  • 方括号 [ ] 表示这部分可以不写。
  • 省略号 … 表示可以写多个。
  • 选项区分大小写——有些命令里 -r 和 -R 不是一回事。

常见问题

Q. git stash 是做什么的?

把没做完的改动先收起来,让工作区变干净;不加 -u 时未跟踪文件会留在原地,pop 取出后会删掉那条记录,apply 则保留。

Q. 怎么写?

git stash [push] [-u] [-m "<message>"] —— 方括号表示可以省略的部分。

Q. 值得记的选项有几个?

这里列了 8 个,完整列表在 man git。这条命令属于git。

相关命令

man 手册: man git-stash