当你在项目的一部分上已经工作一段时间后,所有东西都进入了混乱的状态,而这时你想要切换到另一个分支做一点别的事情。 问题是,你不想仅仅因为过会儿回到这一点而为做了一半的工作创建一次提交。 针对这个问题的答案是 git stash 命令
Copy $ git status
Changes to be committed:
( use "git reset HEAD <file>..." to unstage )
modified: index.html
Changes not staged for commit:
( use "git add <file>..." to update what will be committed )
( use "git checkout -- <file>..." to discard changes in working directory )
modified: lib/simplegit.rb
Copy git stash
Saved working directory and index state WIP on master: 84dfd50 this is change at github master
Copy git status
# On branch master
nothing to commit, working directory clean
Copy $ git stash list
stash@ {0} : WIP on master: 84dfd50 added the index file
stash@ {1} : WIP on master: c264051 Revert "added file_size"
stash@ {2} : WIP on master: 21d80a5 added number to log
Copy git stash apply [<stash_name>]
在本例中,有两个之前做的储藏,所以你接触到了三个不同的储藏工作。 可以通过原来 stash 命令的帮助提示中的命令将你刚刚储藏的工作重新应用:git stash apply。
Copy git stash apply
# On branch master
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
#
# modified: index.html
# modified: lib/simplegit.rb
#
如果想要应用其中一个更旧的储藏,可以通过名字指定它,像这样:git stash apply stash@{2}。 如果不指定一个储藏,Git 认为指定的是最近的储藏
Copy git stash drop [<stash_name>]
Copy 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
$ git stash drop stash@{ 0 }
Dropped stash@{ 0 } (364e91f3f268f0900bc3ee613f9f733e82aaed43)