- Stashing
- 储藏工作
- 应用储藏
- 被暂存的文件重新暂存
- 应用后,移除储藏的内容
- 取消储藏(Un-applying a Stash)
- 新建stash-unapply别名
- 从储藏中创建分支
原文
场景:当项目中某一部分正在编码中,突然接到新任务,又必须换至其他分支去完成。
问题:你不想提交进行了一半的工作,否则以后你无法回到这个工作点。
解决:**git stash **命令。
“Stashing”可以获取工作目录的中间状态,即:将修改过的被追踪的文件和暂存的变更,保存到一个未完结变更的堆栈中,随时可以重新应用。
- 进入项目目录,修改某个文件,有可能还暂存其中的一个变更。
- **git status **命令,查看中间状态:
{%codeblock lang:bash %}
$ git status
On branch master
Changes to be committed:
(use “git reset HEAD …” to unstage)
modified: index.html
Changes not staged for commit:
(use “git add …” to update what will be committed)
modified: lib/simplegit.rb
{%endcodeblock%}
3. 切换分支,但不提交step 1 中的变更,所以储藏这些变更。
执行**git stash 命令,往堆栈中推送一个新的储藏:
{%codeblock lang:bash %}
$ git stash
Saved working directory and index state
“WIP on master: 049d078 added the index file”
HEAD is now at 049d078 added the index file
(To restore them type “git stash apply”)
{%endcodeblock%}
4. 执行step 2查看目录库,中间状态就不见了:
{%codeblock lang:bash %}
$ git status
#######On branch master
nothing to commit, working directory clean
{%endcodeblock%}
这时,你可以方便地切换到其他分支工作;你的变更都保存在栈上。
5. 使用git stash list**要查看现有的储藏:
{%codeblock lang:bash%}
$ git stash list
stash@{0}: WIP on master: 049d078 added the index file
stash@{1}: WIP on master: c264051 Revert “added file_size”
stash@{2}: WIP on master: 21d80a5 added number to log
{%endcodeblock%}
在这个案例中,之前已经进行了两次储藏,所以你可以访问到三个不同的储藏。
执行**git stash apply命令, 可以重新应用最近的一次储藏;
执行git stash apply stash@{2}**命令,即通过指定储藏的名字,来应用更早的储藏。
{%codeblock lang:bash%}
$ git stash apply
On branch master
Changes not staged for commit:
(use “git add …” to update what will be committed)
modified: index.html
modified: lib/simplegit.rb
{%endcodeblock%}