git stash使用教程

git stash用于將當前工作區(qū)的修改暫存起來,就像堆棧一樣,可以隨時將某一次緩存的修改再重新應(yīng)用到當前工作區(qū)。一旦用好了這個命令,會極大提高工作效率。
舉例說明:

1、準備工作,首先初始化一個git倉

隨便建立一個目錄,進去,然后使用 :
$: git init .
添加一個文件:
$: touch hello
$: git add .
$: git commit -m "first add"

2、暫存當前修改內(nèi)容(git stash)

假設(shè)我們在寫一個C函數(shù),如下:
$:~/code/linux/git$ vim hello.c 
$:~/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();}

調(diào)試OK,發(fā)現(xiàn)func1功能OK,但是應(yīng)該優(yōu)化一下,可能效率更高,這個時候怎么辦?
直接改func1的話,如果發(fā)現(xiàn)修改不合適,想回退的話很麻煩,這個時候可以用git stash將將修改暫存起來。

$: ~/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
$:~/code/linux/git$ git status
On branch master
nothing to commit, working directory clean

3、彈出修改內(nèi)容(git stash pop)

這個時候你重新編寫func1, 發(fā)現(xiàn)效果不好,后悔了,于是可以用git stash pop命令,彈出剛才的內(nèi)容(注意先用git checkout . 清空工作區(qū))

$:~/code/linux/git$ vim hello.c 
$:~/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....
$:~/code/linux/git$ git checkout .
$:~/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)
$:~/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 彈出成功后,暫存列表里面就沒有了,如果當前工作區(qū)不干凈,彈出時有沖突,則暫存列表會繼續(xù)保留修改。

4、可以保存多個修改

假設(shè)你在實現(xiàn)一個功能,有好幾種算法可以實現(xiàn),你想逐個嘗試看效果。
現(xiàn)在你在func1中實現(xiàn)了一種方法,準備嘗試寫func2,用另一種方法。
那么可以將func1的修改入棧,去寫fun2,等fun2寫好后,你又想試試func3,那么沒關(guān)系,可以用同樣的方法保存func2的修改:

$:~/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();}
$:~/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
$:~/code/linux/git$ git status
On branch master
nothing to commit, working directory clean
$:~/code/linux/git$ vim hello.c 
$:~/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();}
$:~/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
$:~/code/linux/git$ git status
On branch master
nothing to commit, working directory clean

5、查看保存的內(nèi)容列表(git stash list)

現(xiàn)在我們保存了兩個修改,一個func1,一個func2,可以通過git stash list去查看保存內(nèi)容列表:

$:~/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

可以清楚的看到這兩次修改,stash@{0}和stash@{1}, 那么哪個對應(yīng)func1,哪個對應(yīng)func2的修改呢?
這時我們需要使用git stash show stash@{X}命令來查看,其中‘X’表示列表號。

$:~/code/linux/git$ git show stash@{0}
commit 72e6a391bcad186ab24676aa1db8d5831c99cec9
Merge: 452b08d 6c95c30
Author: hiekay
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();}
$:~/code/linux/git$ git show stash@{1}
commit 7fcca4b66640c51ca76e637df03264b7c41885be
Merge: 452b08d 1c37881
Author: hiekay
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();}

發(fā)現(xiàn)stash@{0}對應(yīng)func2的修改, stash@{1}對應(yīng)func1的修改,原來新入棧的修改,其代號為0,循環(huán)命名。

6、應(yīng)用任意一次修改到當前目錄(git apply stash@{x})

如果現(xiàn)在又想回到func1的修改,怎么辦呢?在工作區(qū)干凈的情況下,要使用git stash apply stash@{1}。
注意這時不能使用git stash pop, 它將最棧頂,即stash@{0}的修改彈出來,而func1現(xiàn)在已經(jīng)是stash@{1}了。

$:~/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")
$:~/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可以將列表中任何一次修改應(yīng)用到當前工作區(qū),我們再次git stash list一把:

$:~/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

我們發(fā)現(xiàn),雖然func1的修改已經(jīng)被彈出應(yīng)用到當前工作區(qū),其修改內(nèi)容還繼續(xù)保留在暫存列表,并未丟棄。
當然,我們可以使用git stash drop stash@{1}來丟掉stash@{1}

7、保存時打標記(git stash save)

假設(shè)現(xiàn)在我們又開始嘗試寫func3, 這樣越來越多,這樣列表會越來越大,你要想搞清楚某次修改對應(yīng)哪個函數(shù),就要挨個用git stash show看一遍,很麻煩。
那么,這個時候git stash 的save參數(shù)就有用了,它可以為這次暫存做個標記,使得你用git stash list的時候顯示這些標記,方便你回憶是修改的什么:

$:~/code/linux/git$ vim hello.c 
$:~/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();}
$:~/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
$:~/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查看時就能知道每一個代號對應(yīng)的修改了。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Git 命令行學習筆記 Git 基礎(chǔ) 基本原理 客戶端并不是只提取最新版本的文件快照,而是把代碼倉庫完整的鏡像下來...
    sunnyghx閱讀 4,163評論 0 11
  • Git 基礎(chǔ) 基本原理 客戶端并不是只提取最新版本的文件快照,而是把代碼倉庫完整的鏡像下來。這樣一來,任何一處協(xié)同...
    __silhouette閱讀 16,214評論 5 147
  • 1.git的安裝 1.1 在Windows上安裝Git msysgit是Windows版的Git,從https:/...
    落魂灬閱讀 12,848評論 4 54
  • git作為時下最流行的代碼管理工具,Git權(quán)威指南總結(jié)了十條喜歡Git的理由: 異地協(xié)同工作; 現(xiàn)場版本控制; 重...
    古斟布衣閱讀 1,906評論 0 12
  • 這些天忙完考試便閑暇了。晚上和閨蜜阿7逛校園,阿7問我大學這幾年里有沒有最遺憾的事兒。仔細想想這幾年里沒有很遺憾...
    石榴柚子啊閱讀 268評論 2 1

友情鏈接更多精彩內(nèi)容