git stash 解决Git切换分支不想提交代码
What?
能够将所有未提交的修改(工作区和暂存区)保存至堆栈中,用于后续恢复当前工作目录。
Why?
当你在新分支开发功能时,遇到突然要去别的分支修改bug或者实现功能时, 会导致git频繁的commit提交 导致
不方便查看做出的修改
& 远程仓库形成一些无用的commit
;
How to do?
命令 | 作用 |
---|---|
git stash | 保存 |
git stash pop | 提取上次保存并删除 |
git stash list | 查看保存的列表 |
git stash apply | 将堆栈中的内容应用到当前目录, 不会将内容从堆栈中删除 |
git stash apply name | 提取列表内名字为name的内容 |
git stash clear | 清除堆栈中的所有内容 |
git stash drop + name | 从堆栈中移除某个指定的stash |
git stash branch | 从最新的stash创建分支。 |
git stash save “name”
存储时 添加name, 方便提取
stash@{0}: On master: name
git stash branch
应用场景:
当储藏了部分工作, 但是应用到某个分支时发生冲突 恢复失败时 可以通过git stash branch
创建新的分支来解决。