git stash
目录
当你正在进行一些开发,但是需要暂时去处理一下项目的其他分支时,你可能需要用到这个东西
stash v. 存放;藏起来
git stash 可以将你当前的修改,存储到一个单独的堆栈中,这个堆栈独立于任意的分支,并且如果你需要的话可以在任意分支上应用堆栈上某个修改
命令
git stash list | 查看堆栈
bash
stash@{0}: On main: 开发中,保存全部修改
stash@{1}: filter-branch: rewrite
stash@{2}: WIP on main: 1ss76 xxxx
- 堆栈后面的需要永远按照 0,1,2,3...的顺序排列
- 如果删掉了某个堆栈,那么堆栈后面的堆栈会自动往前移
git stash push | 添加堆栈
bash
# 将当前修改添加到堆栈中
git stash push -m "开发中,保存全部修改"
# 将当前修改和添加文件添加到堆栈中
git stash push -u -m "开发中,保存全部改动"
# 将.gitignore 忽略的文件也添加到堆栈中
git stash push --all -m "开发中,保存全部改动"
- git stash push 默认行为
- 将缓存区中的修改添加到堆栈中
- 将工作区中的修改添加到堆栈中
- 不会将新建的文件(未追踪的文件)添加到堆栈,需要通过
-u
参数来添加 - 使用
--all
参数,将所有文件添加到堆栈中
git stash pop | 恢复堆栈
bash
# 恢复最后一个堆栈
git stash pop
# 等价于
git stash pop stash@{0}
# 恢复指定堆栈
git stash pop stash@{1}
- 恢复堆栈之后,堆栈会被删除,堆栈中的修改会被恢复到工作区中
- 可以恢复任意堆栈到当前的分支,即使这个堆栈是从另一个分支创建的
git stash apply | 应用堆栈
相比与 pop,apply 更适合用于尝试当前堆栈是否合适
- apply 将堆栈中的修改应用到工作区中,但是不会删除堆栈
但是请注意 apply 这个操作是不可撤销的,比如你已经有一堆修改了,然后接受了一个堆栈,这个堆栈中的内容会合原本的缓存混合在一起,就比较难撤回了
git stash drop | 删除堆栈
bash
# 删除最后一个堆栈
git stash drop
# 删除指定堆栈
git stash drop stash@{1}
# 删除所有堆栈
git stash drop stash