git stash用于将当前工作区的修改暂存起来,就像堆栈一样,可以随时将某一次缓存的修改再重新应用到当前工作区。
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 100644 --- a/hello.c +++ b/hello.c @@ -0,0 +1,2 @@ +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..9c5bff3 100644 --- a/hello.c +++ b/hello.c @@ -0,0 +1 @@ +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@{0} (208ca2e2c0c455da554986a6770a74ad0de5b1e0) yang@Ubuntu64:~/code/linux/git$ git diff diff --git a/hello.c b/hello.c index e69de29..bdc92a5 100644 --- a/hello.c +++ b/hello.c @@ -0,0 +1,2 @@ +void func1(void) {printf("this is func1");} +void main(void) {return func1();}
注意,git stash pop 弹出成功后,暂存列表里面就没有了,如果当前工作区不干净,弹出时有冲突,则暂存列表会继续保留修改。
yang@Ubuntu64:~/code/linux/git$ git diff diff --git a/hello.c b/hello.c index e69de29..bdc92a5 100644 --- a/hello.c +++ b/hello.c @@ -0,0 +1,2 @@ +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 100644 --- a/hello.c +++ b/hello.c @@ -0,0 +1,2 @@ +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
yang@Ubuntu64:~/code/linux/git$ git stash list stash@{0}: WIP on master: 452b08d rename hello as hello.c stash@{1}: WIP on master: 452b08d rename hello as hello.c
yang@Ubuntu64:~/code/linux/git$ git show stash@{0}
commit 72e6a391bcad186ab24676aa1db8d5831c99cec9
Merge: 452b08d 6c95c30
Author: NickYang <yang@Ubuntu64>
Date: Sat Mar 12 19:56:18 2016 +0800
WIP on master: 452b08d rename hello as hello.c
diff --cc hello.c
index e69de29,e69de29..7fd0a13
--- a/hello.c
+++ b/hello.c
@@@ -1,0 -1,0 +1,2 @@@
++void func2(void) {printf("this is func2");}
++void main(void) {return func2();}
yang@Ubuntu64:~/code/linux/git$ git show stash@{1}
commit 7fcca4b66640c51ca76e637df03264b7c41885be
Merge: 452b08d 1c37881
Author: NickYang <yang@Ubuntu64>
Date: Sat Mar 12 19:54:35 2016 +0800
WIP on master: 452b08d rename hello as hello.c
diff --cc hello.c
index e69de29,e69de29..bdc92a5
--- a/hello.c
+++ b/hello.c
@@@ -1,0 -1,0 +1,2 @@@
++void func1(void) {printf("this is func1");}
++void main(void) {return func1();}
发现stash@{0}对应func2的修改, stash@{1}对应func1的修改,原来新入栈的修改,其代号为0,循环命名。
yang@Ubuntu64:~/code/linux/git$ git stash apply stash@{1}
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 100644
--- a/hello.c
+++ b/hello.c
@@ -0,0 +1,2 @@
+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@{0}: WIP on master: 452b08d rename hello as hello.c stash@{1}: WIP on master: 452b08d rename hello as hello.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..786c214 100644 --- a/hello.c +++ b/hello.c @@ -0,0 +1,2 @@ +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@{0}: On master: this is func3 stash@{1}: WIP on master: 452b08d rename hello as hello.c stash@{2}: WIP on master: 452b08d rename hello as hello.c
我们在save后面指定一个字符串,作为提醒,这样在git stash list查看时就能知道每一个代号对应的修改了。
原文:http://www.cnblogs.com/yanghaizhou/p/5269899.html