git stash
假设这样一种场景:
你正在一个分支上开发,还没有开发完成,然而代码并不想提交,需要切换到另一个分支进行开发,这个就可以stash 暂存本地的更改,让工作区保证和上次提交的内容一致。当你重新切到当前的分支上后,还可以重新获取暂存的内容。
如下显示本地的分支,git branch
Lenovo@LENOVO-PC /c/WorkSpace5-gitosc/HibernateTest (req/cnivi_111) $ git branch master req/cnivi_100 * req/cnivi_111
在当前的分支 下修改一个文件,如下,
$ git status On branch req/cnivi_111 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: Gender.java Untracked files: (use "git add <file>..." to include in what will be committed) ../../../../../BaseDao-UML.png no changes added to commit (use "git add" and/or "git commit -a")
现在要切换到另一个分支,如下,
Lenovo@LENOVO-PC /c/WorkSpace5-gitosc/HibernateTest/src/main/java/com/lyx (req/cnivi_111) $ git checkout req/cnivi_100 M src/main/java/com/lyx/Gender.java Switched to branch 'req/cnivi_100'
看一下当前分支的状态,
Lenovo@LENOVO-PC /c/WorkSpace5-gitosc/HibernateTest/src/main/java/com/lyx (req/cnivi_100) $ git status On branch req/cnivi_100 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: Gender.java Untracked files: (use "git add <file>..." to include in what will be committed) ../../../../../BaseDao-UML.png no changes added to commit (use "git add" and/or "git commit -a")
可以看到当前分支 100 里会有 分支 111 里的改动信息。。。。。
这是什么原因呢 ?
http://segmentfault.com/q/1010000000156026
其背后的原因:一个本地的git repo只有一个工作区和暂存区,但是有多个分支的提交区,而我们的checkout只是将HEAD指针从一个分支切换到另一个分支。
所以这种情况可以使用stash 暂存当前工作区的更改。然后切换到另一个分支,
如下,回到分支 111,
Lenovo@LENOVO-PC /c/WorkSpace5-gitosc/HibernateTest/src/main/java/com/lyx (req/cnivi_111) $ git status On branch req/cnivi_111 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: Gender.java Untracked files: (use "git add <file>..." to include in what will be committed) ../../../../../BaseDao-UML.png no changes added to commit (use "git add" and/or "git commit -a") Lenovo@LENOVO-PC /c/WorkSpace5-gitosc/HibernateTest/src/main/java/com/lyx (req/c nivi_111) $ git stash Saved working directory and index state WIP on req/cnivi_111: 1c45053 add HEAD is now at 1c45053 add
运行git stash 命令后,切换分支到 100 ,如下,
Lenovo@LENOVO-PC /c/WorkSpace5-gitosc/HibernateTest/src/main/java/com/lyx (req/cnivi_100) $ git status On branch req/cnivi_100 Untracked files: (use "git add <file>..." to include in what will be committed) ../../../../../BaseDao-UML.png nothing added to commit but untracked files present (use "git add" to track)
可以看到 分支 111 工作区的更改 没有在分支 100 上显示出来,因为已经 git stash了。。。
那么切回 分支 111 ,取回之前stash 的内容。
Lenovo@LENOVO-PC /c/WorkSpace5-gitosc/HibernateTest/src/main/java/com/lyx (req/cnivi_111) $ git stash list stash@{0}: WIP on req/cnivi_111: 1c45053 add
通过stash list 可以看到多次stash 的内容,如上。通过git stash apply stashName 命令可以取回相应的暂存的更改。
Lenovo@LENOVO-PC /c/WorkSpace5-gitosc/HibernateTest/src/main/java/com/lyx (req/c nivi_111) $ git stash apply stash@{0} On branch req/cnivi_111 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: Gender.java Untracked files: (use "git add <file>..." to include in what will be committed) ../../../../../BaseDao-UML.png no changes added to commit (use "git add" and/or "git commit -a")
可以看到已经取回了相应的暂存的更改。
还有其他一下命令,
git stash pop: 从Git栈中读取最近一次保存的内容,恢复工作区的相关内容。由于可能存在多个Stash的内容,所以用栈来管理,pop会从最近的一个stash中读取内容并恢复。
git stash list: 显示Git栈内的所有备份,可以利用这个列表来决定从那个地方恢复。
git stash clear: 清空Git栈。此时使用gitg等图形化工具会发现,原来stash的哪些节点都消失了。
参考:http://segmentfault.com/q/1010000000156026
http://www.cppblog.com/deercoder/archive/2011/11/13/160007.html
======================END======================