git stash使用教程

git stash用于將當(dāng)前工作區(qū)的修改暫存起來(lái)抽诉,就像堆棧一樣礁凡,可以隨時(shí)將某一次緩存的修改再重新應(yīng)用到當(dāng)前工作區(qū)。一旦用好了這個(gè)命令孽亲,會(huì)極大提高工作效率坎穿。
舉例說(shuō)明:

1、準(zhǔn)備工作,首先初始化一個(gè)git倉(cāng)

隨便建立一個(gè)目錄玲昧,進(jìn)去栖茉,然后使用 :
$: git init .
添加一個(gè)文件:
$: touch hello
$: git add .
$: git commit -m "first add"

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

假設(shè)我們?cè)趯懸粋€(gè)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)化一下尘应,可能效率更高惶凝,這個(gè)時(shí)候怎么辦?
直接改func1的話犬钢,如果發(fā)現(xiàn)修改不合適苍鲜,想回退的話很麻煩,這個(gè)時(shí)候可以用git stash將將修改暫存起來(lái)娜饵。

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

這個(gè)時(shí)候你重新編寫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 彈出成功后愿伴,暫存列表里面就沒(méi)有了,如果當(dāng)前工作區(qū)不干凈电湘,彈出時(shí)有沖突隔节,則暫存列表會(huì)繼續(xù)保留修改。

4寂呛、可以保存多個(gè)修改

假設(shè)你在實(shí)現(xiàn)一個(gè)功能怎诫,有好幾種算法可以實(shí)現(xiàn),你想逐個(gè)嘗試看效果贷痪。
現(xiàn)在你在func1中實(shí)現(xiàn)了一種方法幻妓,準(zhǔn)備嘗試寫func2,用另一種方法劫拢。
那么可以將func1的修改入棧肉津,去寫fun2,等f(wàn)un2寫好后舱沧,你又想試試func3妹沙,那么沒(méi)關(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)在我們保存了兩個(gè)修改距糖,一個(gè)func1玄窝,一個(gè)func2,可以通過(guò)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}哆料, 那么哪個(gè)對(duì)應(yīng)func1缸剪,哪個(gè)對(duì)應(yīng)func2的修改呢吗铐?
這時(shí)我們需要使用git stash show stash@{X}命令來(lái)查看,其中‘X’表示列表號(hào)杏节。

$:~/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}對(duì)應(yīng)func2的修改唬渗, stash@{1}對(duì)應(yīng)func1的修改,原來(lái)新入棧的修改奋渔,其代號(hào)為0镊逝,循環(huán)命名。

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

如果現(xiàn)在又想回到func1的修改撑蒜,怎么辦呢?在工作區(qū)干凈的情況下玄渗,要使用git stash apply stash@{1}座菠。
注意這時(shí)不能使用git stash pop, 它將最棧頂藤树,即stash@{0}的修改彈出來(lái)浴滴,而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();}

可見(jiàn)git stash apply可以將列表中任何一次修改應(yīng)用到當(dāng)前工作區(qū)岁钓,我們?cè)俅蝕it 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)用到當(dāng)前工作區(qū),其修改內(nèi)容還繼續(xù)保留在暫存列表屡限,并未丟棄品嚣。
當(dāng)然,我們可以使用git stash drop stash@{1}來(lái)丟掉stash@{1}

7钧大、保存時(shí)打標(biāo)記(git stash save)

假設(shè)現(xiàn)在我們又開(kāi)始嘗試寫func3翰撑, 這樣越來(lái)越多,這樣列表會(huì)越來(lái)越大拓型,你要想搞清楚某次修改對(duì)應(yīng)哪個(gè)函數(shù)额嘿,就要挨個(gè)用git stash show看一遍,很麻煩劣挫。
那么册养,這個(gè)時(shí)候git stash 的save參數(shù)就有用了,它可以為這次暫存做個(gè)標(biāo)記压固,使得你用git stash list的時(shí)候顯示這些標(biāo)記球拦,方便你回憶是修改的什么:

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

我們?cè)趕ave后面指定一個(gè)字符串,作為提醒,這樣在git stash list查看時(shí)就能知道每一個(gè)代號(hào)對(duì)應(yīng)的修改了坎炼。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末愧膀,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子谣光,更是在濱河造成了極大的恐慌檩淋,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,000評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件萄金,死亡現(xiàn)場(chǎng)離奇詭異蟀悦,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)氧敢,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,745評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門日戈,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人孙乖,你說(shuō)我怎么就攤上這事浙炼。” “怎么了唯袄?”我有些...
    開(kāi)封第一講書人閱讀 168,561評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵弯屈,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我越妈,道長(zhǎng)季俩,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書人閱讀 59,782評(píng)論 1 298
  • 正文 為了忘掉前任梅掠,我火速辦了婚禮酌住,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘阎抒。我一直安慰自己酪我,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,798評(píng)論 6 397
  • 文/花漫 我一把揭開(kāi)白布且叁。 她就那樣靜靜地躺著都哭,像睡著了一般。 火紅的嫁衣襯著肌膚如雪逞带。 梳的紋絲不亂的頭發(fā)上欺矫,一...
    開(kāi)封第一講書人閱讀 52,394評(píng)論 1 310
  • 那天,我揣著相機(jī)與錄音展氓,去河邊找鬼穆趴。 笑死,一個(gè)胖子當(dāng)著我的面吹牛遇汞,可吹牛的內(nèi)容都是我干的未妹。 我是一名探鬼主播簿废,決...
    沈念sama閱讀 40,952評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼络它!你這毒婦竟也來(lái)了族檬?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書人閱讀 39,852評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤化戳,失蹤者是張志新(化名)和其女友劉穎单料,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體迂烁,經(jīng)...
    沈念sama閱讀 46,409評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡看尼,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,483評(píng)論 3 341
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了盟步。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,615評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡躏结,死狀恐怖却盘,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情媳拴,我是刑警寧澤黄橘,帶...
    沈念sama閱讀 36,303評(píng)論 5 350
  • 正文 年R本政府宣布,位于F島的核電站屈溉,受9級(jí)特大地震影響塞关,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜子巾,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,979評(píng)論 3 334
  • 文/蒙蒙 一帆赢、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧线梗,春花似錦椰于、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 32,470評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至烤咧,卻和暖如春偏陪,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背煮嫌。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 33,571評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工笛谦, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人立膛。 一個(gè)月前我還...
    沈念sama閱讀 49,041評(píng)論 3 377
  • 正文 我出身青樓揪罕,卻偏偏與公主長(zhǎng)得像梯码,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子好啰,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,630評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容

  • Git 命令行學(xué)習(xí)筆記 Git 基礎(chǔ) 基本原理 客戶端并不是只提取最新版本的文件快照轩娶,而是把代碼倉(cāng)庫(kù)完整的鏡像下來(lái)...
    sunnyghx閱讀 3,929評(píng)論 0 11
  • Git 基礎(chǔ) 基本原理 客戶端并不是只提取最新版本的文件快照,而是把代碼倉(cāng)庫(kù)完整的鏡像下來(lái)框往。這樣一來(lái)鳄抒,任何一處協(xié)同...
    __silhouette閱讀 15,899評(píng)論 5 147
  • 1.git的安裝 1.1 在Windows上安裝Git msysgit是Windows版的Git,從https:/...
    落魂灬閱讀 12,668評(píng)論 4 54
  • git作為時(shí)下最流行的代碼管理工具椰弊,Git權(quán)威指南總結(jié)了十條喜歡Git的理由: 異地協(xié)同工作许溅; 現(xiàn)場(chǎng)版本控制; 重...
    古斟布衣閱讀 1,826評(píng)論 0 12
  • 這些天忙完考試便閑暇了。晚上和閨蜜阿7逛校園清焕,阿7問(wèn)我大學(xué)這幾年里有沒(méi)有最遺憾的事兒并蝗。仔細(xì)想想這幾年里沒(méi)有很遺憾...
    石榴柚子啊閱讀 217評(píng)論 2 1