git stash用于将当前工作区的修改暂存起来,就像堆栈一样,可以随时将某一次缓存的修改再重新应用到当前工作区。

一旦用好了这个命令,会极大提高工作效率。
 
直接举例说明:
1、准备工作,首先初始化一个git仓
    随便建立一个目录,进去,然后使用 :
    $: git init .
    添加一个文件:
    $: touch hello
    $: git add .
    $: git commit -m "first add"
 
2、暂存当前修改内容(git stash)
    假设我们在写一个C函数,如下:
yang@Ubuntu64:~/code/linux/git$ vim hello.c
yang@Ubuntu64:~/code/linux/git$ git diff
diff --git a/hello.c b/hello.c
index e69de29..bdc92a5
--- a/hello.c
+++ b/hello.c
@@ -, +, @@
+void func1(void) {printf("this is func1");}
+void main(void) {return func1();}
 调试OK,发现func1功能OK,但是应该优化一下,可能效率更高,这个时候怎么办?
 直接改func1的话,如果发现修改不合适,想回退的话很麻烦,这个时候可以用git stash将将修改暂存起来。
yang@Ubuntu64:~/code/linux/git$ git stash
Saved working directory and index state WIP on master: 452b08d rename hello as hello.c
HEAD is now at 452b08d rename hello as hello.c
yang@Ubuntu64:~/code/linux/git$ git status
On branch master
nothing to commit, working directory clean
 
3、弹出修改内容(git stash pop)
  这个时候你重新编写func1, 发现效果不好,后悔了,于是可以用git stash pop命令,弹出刚才的内容(注意先用git checkout . 清空工作区)
 

yang@Ubuntu64:~/code/linux/git$ vim hello.c
yang@Ubuntu64:~/code/linux/git$ git diff
diff --git a/hello.c b/hello.c
index e69de29..9c5bff3
--- a/hello.c
+++ b/hello.c
@@ -, + @@
+some bad chenges....
yang@Ubuntu64:~/code/linux/git$ git checkout .
yang@Ubuntu64:~/code/linux/git$ git stash pop
On branch master
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: hello.c no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{} (208ca2e2c0c455da554986a6770a74ad0de5b1e0)
yang@Ubuntu64:~/code/linux/git$ git diff
diff --git a/hello.c b/hello.c
index e69de29..bdc92a5
--- a/hello.c
+++ b/hello.c
@@ -, +, @@
+void func1(void) {printf("this is func1");}
+void main(void) {return func1();}

注意,git stash pop 弹出成功后,暂存列表里面就没有了,如果当前工作区不干净,弹出时有冲突,则暂存列表会继续保留修改。

4、可以保存多个修改
  假设你在实现一个功能,有好几种算法可以实现,你想逐个尝试看效果。
  现在你在func1中实现了一种方法,准备尝试写func2,用另一种方法。
  那么可以将func1的修改入栈,去写fun2,等fun2写好后,你又想试试func3,那么没关系,可以用同样的方法保存func2的修改:

yang@Ubuntu64:~/code/linux/git$ git diff
diff --git a/hello.c b/hello.c
index e69de29..bdc92a5
--- a/hello.c
+++ b/hello.c
@@ -, +, @@
+void func1(void) {printf("this is func1");}
+void main(void) {return func1();}
yang@Ubuntu64:~/code/linux/git$ git stash
Saved working directory and index state WIP on master: 452b08d rename hello as hello.c
HEAD is now at 452b08d rename hello as hello.c
yang@Ubuntu64:~/code/linux/git$ git status
On branch master
nothing to commit, working directory clean
yang@Ubuntu64:~/code/linux/git$ vim hello.c
yang@Ubuntu64:~/code/linux/git$ git diff
diff --git a/hello.c b/hello.c
index e69de29..7fd0a13
--- a/hello.c
+++ b/hello.c
@@ -, +, @@
+void func2(void) {printf("this is func2");}
+void main(void) {return func2();}
yang@Ubuntu64:~/code/linux/git$ git stash
Saved working directory and index state WIP on master: 452b08d rename hello as hello.c
HEAD is now at 452b08d rename hello as hello.c
yang@Ubuntu64:~/code/linux/git$ git status
On branch master
nothing to commit, working directory clean
5、查看保存的内容列表(git stash list)
  现在我们保存了两个修改,一个func1,一个func2,可以通过git stash list去查看保存内容列表:

yang@Ubuntu64:~/code/linux/git$ git stash list
stash@{}: WIP on master: 452b08d rename hello as hello.c
stash@{}: WIP on master: 452b08d rename hello as hello.c
  可以清楚的看到这两次修改,stash@{0}和stash@{1}, 那么哪个对应func1,哪个对应func2的修改呢?
  这时我们需要使用git stash show stash@{X}命令来查看,其中‘X’表示列表号。
yang@Ubuntu64:~/code/linux/git$ git show stash@{}
commit 72e6a391bcad186ab24676aa1db8d5831c99cec9
Merge: 452b08d 6c95c30
Author: NickYang <yang@Ubuntu64>
Date: Sat Mar :: + WIP on master: 452b08d rename hello as hello.c diff --cc hello.c
index e69de29,e69de29..7fd0a13
--- a/hello.c
+++ b/hello.c
@@@ -, -, +, @@@
++void func2(void) {printf("this is func2");}
++void main(void) {return func2();}
yang@Ubuntu64:~/code/linux/git$ git show stash@{}
commit 7fcca4b66640c51ca76e637df03264b7c41885be
Merge: 452b08d 1c37881
Author: NickYang <yang@Ubuntu64>
Date: Sat Mar :: + WIP on master: 452b08d rename hello as hello.c diff --cc hello.c
index e69de29,e69de29..bdc92a5
--- a/hello.c
+++ b/hello.c
@@@ -, -, +, @@@
++void func1(void) {printf("this is func1");}
++void main(void) {return func1();}

发现stash@{0}对应func2的修改, stash@{1}对应func1的修改,原来新入栈的修改,其代号为0,循环命名。

6、应用任意一次修改到当前目录(git apply stash@{x})
 如果现在又想回到func1的修改,怎么办呢?在工作区干净的情况下,要使用git stash apply stash@{1}。
 注意这时不能使用git stash pop, 它将最栈顶,即stash@{0}的修改弹出来,而func1现在已经是stash@{1}了。

yang@Ubuntu64:~/code/linux/git$ git stash apply stash@{}
On branch master
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: hello.c no changes added to commit (use "git add" and/or "git commit -a")
yang@Ubuntu64:~/code/linux/git$ git diff
diff --git a/hello.c b/hello.c
index e69de29..bdc92a5
--- a/hello.c
+++ b/hello.c
@@ -, +, @@
+void func1(void) {printf("this is func1");}
+void main(void) {return func1();}

可见git stash apply可以将列表中任何一次修改应用到当前工作区,我们再次git stash list一把:

yang@Ubuntu64:~/code/linux/git$ git stash list
stash@{}: WIP on master: 452b08d rename hello as hello.c
stash@{}: WIP on master: 452b08d rename hello as hello.c
  我们发现,虽然func1的修改已经被弹出应用到当前工作区,其修改内容还继续保留在暂存列表,并未丢弃。
  当然,我们可以使用git stash drop stash@{1}来丢掉stash@{1}
 
7、保存时打标记(git stash save)
  假设现在我们又开始尝试写func3, 这样越来越多,这样列表会越来越大,你要想搞清楚某次修改对应哪个函数,就要挨个用git stash show看一遍,很麻烦。
  那么,这个时候git stash 的save参数就有用了,它可以为这次暂存做个标记,使得你用git stash list的时候显示这些标记,方便你回忆是修改的什么:
yang@Ubuntu64:~/code/linux/git$ vim hello.c
yang@Ubuntu64:~/code/linux/git$ git diff
diff --git a/hello.c b/hello.c
index e69de29..786c214
--- a/hello.c
+++ b/hello.c
@@ -, +, @@
+void func3(void) {printf("this is func3");}
+void main(void) {return func3();}
yang@Ubuntu64:~/code/linux/git$ git stash save "this is func3"
Saved working directory and index state On master: this is func3
HEAD is now at 452b08d rename hello as hello.c
yang@Ubuntu64:~/code/linux/git$ git stash list
stash@{}: On master: this is func3
stash@{}: WIP on master: 452b08d rename hello as hello.c
stash@{}: WIP on master: 452b08d rename hello as hello.c

我们在save后面指定一个字符串,作为提醒,这样在git stash list查看时就能知道每一个代号对应的修改了。

git stash 用法的更多相关文章

  1. 转:Git: git stash 用法小结

    一.应用场景 综合下网上的介绍和资料, git stash (git储藏)可用于以下情形: 发现有一个类是多余的,想删掉它又担心以后需要查看它的代码,想保存它但又不想增加一个脏的提交.这时就可以考虑  ...

  2. git stash用法

    使用场景: 当前修改的代码还不足以提交commit,但又必须切换到其他分支,要想完成这样的操作就可以使用git stash git stash意思就是备份当前的工作区的内容,从最近的一次提交中读取相关 ...

  3. git stash 用法总结和注意点

    常用git stash命令: (1)git stash save "save message"  : 执行存储时,添加备注,方便查找,只有git stash 也要可以的,但查找时不 ...

  4. git stash应用

    今天在看一个bug,之前一个分支的版本是正常的,在新的分支上上加了很多日志没找到原因,希望回溯到之前的版本,确定下从哪个提交引入的问题,但是还不想把现在的修改提交,也不希望在Git上看到当前修改的版本 ...

  5. 用好Git stash,助你事半功倍

    git stash: 用法:git stash list [<选项>] 或:git stash show [<选项>] [<stash>] 或:git stash ...

  6. git stash命令及提交指定文件

    一.git stash命令 常用git stash命令: (1)git stash save "save message" : 执行存储时,添加备注,方便查找,只有git stas ...

  7. Git stash 常见用法

    Git stash git stash这个命令可以将当前的工作状态保存到git栈,在需要的时候再恢复 1.1 git stash  保存当前的工作区与暂存区的状态,把当前的工作隐藏起来,等以后需要的时 ...

  8. git stash的用法

    使用git stash git stash的使用场景是这样的: 当你正在你的分支下进行开发时,这时候你可能需要切换到你的另一个分支去,你可能要pull新的代码下来,但是你又不想添加无用的commit. ...

  9. git stash解决代码merge出错

    最近在使用git提交代码时,遇到一个问题,就是我修改了几个文件的代码,然后又想把自己代码库里面的代码更新到最新版本,然后不出所料,代码冲突了!作为一个喜欢解决问题的程序员,怎么会被这样的问题所困住呢? ...

随机推荐

  1. 万能的SqlHelper,麻麻再也不用担心用什么数据库了

    以前只用一种数据库,倒也无所谓,但是再数据库切换的时候,发现代码差不多呀. 最初,两种数据库,大不了写两个SqlHelper,但是多了也就发现代码重用率太低了吧. 因此,下面的SqlHelper诞生了 ...

  2. 【C#进阶系列】22 CLR寄宿和AppDomain

    关于寄宿和AppDomain 微软开发CLR时,将它实现成包含在一个DLL中的COM服务器. 任何Windows应用程序都能寄宿(容纳)CLR.(简单来讲,就是CLR在一个DLL中,通过引用这个DLL ...

  3. EC笔记:第二部分:11:在operator=中处理“自我赋值”

    已经一年半没有写过博客了,最近发现学过的知识还是需要整理一下,为知笔记,要开始收费了以前写在为知笔记上笔记也会慢慢的转到博客里. 话不多说,进入正题. 考虑考虑以下场景: 当某个对象对自身赋值时,会出 ...

  4. windows 下使用Nginx替代apache作为服务器

    说实话, 在windows下使用Nginx 着实有点不太方便, 但因项目需求, 又不想换系统(虽然可以搞个虚拟机玩), 只能用Nginx了 好了, 不多说了. 开始... 首先我用的是xampp包(A ...

  5. PHP工作笔记:使用yii migrate管理、生成数据库

    第一步:进入yii migrate 通过dos(我是win7系统,其他系统类似,就是进入字符界面)打开网站目录 phpStudy/WWW/local/ddc_dlss 输入 ./yii migrate ...

  6. SQLServer清空日志

      USE [master]  GO  ALTER DATABASE TRAVEL SET RECOVERY SIMPLE WITH NO_WAIT  GO  ALTER DATABASE TRAVE ...

  7. PresentViewController切换界面

    视图切换,没有NavigationController的情况下,一般会使用presentViewController来切换视图并携带切换时的动画, 其中切换方法如下: – presentViewCon ...

  8. 学习zepto.js(对象方法)[2]

    今天来说下zepto那一套dom操作方法, prepend,append,prependTo,appendTo,before,after,insertBefore,insertAfter; 按着从内到 ...

  9. 在C#开发中如何使用Client Object Model客户端代码获得SharePoint 网站、列表的权限情况

    自从人类学会了使用火,烤制的方式替代了人类的消化系统部分功能,从此人类的消化系统更加简单,加速了人脑的进化:自从SharePoint 2010开始有了Client Side Object Model ...

  10. spring-boot-framework 如何自动将对象返回成json格式

    使用srping-rest-mvc 的时候只要在工程的classpath中包含jackson的2.x版本,就可以不用自己做json格式的转换了. 如在你的pom文件中加入以下的依赖: <depe ...