Git Stash命令的使用

1、使用場景

在遠程倉庫拉取代碼之后,需要修改倉庫中的某些配置文件才能夠正常將工程運行起來奥额。但是苫幢,改動后的文件會影響Git正常的拉取操作(Git會提示先處理本地的改動才可以拉取)访诱。
這時候,比較方便的做法是將本地的修改用stash命令保存韩肝。之后触菜,工作區(qū)就成了一個“干凈”的狀態(tài),可以進行正常的拉取操作哀峻。
拉取完成之后涡相,可以再通過stash命令將之前stash的內(nèi)容再“取出”到工作區(qū)哲泊,這樣就可以重新運行工程。
由于stash保存的內(nèi)容催蝗,可以跨分支進行“取出”切威。在上面的場景中,功能算是比較強大了丙号。

2先朦、git stash介紹

運行git help stash命令,可以看到這個命令的幫助:

NAME
       git-stash - Stash the changes in a dirty working directory away

DESCRIPTION
       Use git stash when you want to record the current state of the working directory and the index, but want to go back to a clean working directory. The command saves your local modifications away and reverts the working directory to match the HEAD commit.
       The modifications stashed away by this command can be listed with git stash list, inspected with git stash show, and restored (potentially on top of a different commit) with git stash apply. Calling git stash without any arguments is equivalent to git stash push. A stash is by default listed as "WIP on branchname ...", but you can give a more descriptive message on the command line when you create one.
       The latest stash you created is stored in refs/stash; older stashes are found in the reflog of this reference and can be named using the usual reflog syntax (e.g. stash@{0} is the most recently created stash, stash@{1} is the one before it, stash@{2.hours.ago} is also possible). Stashes may also be referenced by specifying just the stash index (e.g. the integer n is equivalent to stash@{n}).

意思就是說:

git-stash - 將一個修改后的工作區(qū)中的改動保存起來犬缨,將工作區(qū)恢復到改動前的狀態(tài)喳魏。

具體描述:
當你想要保存工作區(qū)的當前狀態(tài),并想要回到一個干凈的工作目錄時怀薛,可以使用git stash命令刺彩。該命令保存本地修改,并將工作區(qū)恢復到HEAD指向的commit狀態(tài)枝恋。
git stash保存的內(nèi)容可以通過命令“git stash list”列出创倔,可以通過“git stash show”命令查看,可以通過“git stash apply”命令恢復(可以恢復到不同的commit/分支上)鼓择。不加任何參數(shù)調(diào)用“git stash”命令等同于“git stash push”三幻。Stash信息默認展示為"WIP on branchname ...",但是你可以在stash命令執(zhí)行的時候呐能,添加相關(guān)描述性的信息念搬。
創(chuàng)建的最新的stash信息保存在"refs/stash",稍微早一點的stash可以通過這個引用的reflog查看摆出,也可以通過通常的reflog語法命名規(guī)則指代朗徊。比如“stash@{0}”表示最新創(chuàng)建的stash,“stash@{1}”是更早些的stash偎漫。stash也可以只通過序號指代爷恳,比如"n"代表"stash@{n}"。

3象踊、例子

3.1 下面是一個連續(xù)的stash保存并取出的例子

查看最近一次提交:

$ git log -1
commit 1eff7133816e9e77d34c25dd63e017ab899bf490 (HEAD -> master, origin/master)
Author: XiaCheng <xxxxxxx@icloud.com>
Date:   Fri Mar 8 18:57:40 2019 +0800

    add doing mark

改動一個工作區(qū)中的文件:

$ echo "asdf" >> style.css 
$ git status
On branch master
Your branch is up to date with 'origin/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:   style.css

no changes added to commit (use "git add" and/or "git commit -a")

將對工作區(qū)的修改用stash命令保存:

$ git stash 
Saved working directory and index state WIP on master: 1eff713 add doing mark
$ git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

再次修改工作區(qū)中的一個文件:

$ echo "qwer" >> style.css
$ git status
On branch master
Your branch is up to date with 'origin/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:   style.css

no changes added to commit (use "git add" and/or "git commit -a")

用stash命令保存温亲,并添加描述信息:

$ git stash save "add qwer to style.css"
Saved working directory and index state On master: add qwer to style.css

查看stash列表:

$ git stash list
stash@{0}: On master: add qwer to style.css
stash@{1}: WIP on master: 1eff713 add doing mark

恢復最近一次stash的保存內(nèi)容:

$ git stash apply stash@{0}
On branch master
Your branch is up to date with 'origin/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:   style.css

no changes added to commit (use "git add" and/or "git commit -a")
$ git status
On branch master
Your branch is up to date with 'origin/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:   style.css

no changes added to commit (use "git add" and/or "git commit -a")

3.2 git stash跨分支

由于git stash保存的內(nèi)容還沒有提交,所以杯矩,這些內(nèi)容不是基于分支的(和具體分支沒有關(guān)系)栈虚,而是基于工作區(qū)的。下面是一個例子史隆。
接著前面的例子魂务,我們首先將之前的修改復位,然后新建一個分支testBra,并修改style.css:

$ git checkout -- style.css 
$ git checkout -b testBra
Switched to a new branch 'testBra'
localhost:todo_man chengxia$ echo "test branch" >> style.css 
localhost:todo_man chengxia$ git status
On branch testBra
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:   style.css

no changes added to commit (use "git add" and/or "git commit -a")
$ git commit -am "add test content on testBra"
[testBra d9b757d] add test content on testBra
 1 file changed, 1 insertion(+), 1 deletion(-)

接下來粘姜,我們?nèi)〕鲋巴ㄟ^stash命令保存的內(nèi)容:

$ git stash list
stash@{0}: On master: add qwer to style.css
stash@{1}: WIP on master: 1eff713 add doing mark
$ git stash apply stash@{0}
Auto-merging style.css
CONFLICT (content): Merge conflict in style.css
$ git status
On branch testBra
Unmerged paths:
  (use "git reset HEAD <file>..." to unstage)
  (use "git add <file>..." to mark resolution)

    both modified:   style.css

no changes added to commit (use "git add" and/or "git commit -a")
$ vim style.css

這時鬓照,提示沖突如下:


Stash沖突內(nèi)容

3.2.1 git add標識沖突解決

這時候,我們可以采用通常的git add命令標識沖突已經(jīng)解決孤紧,如下:

$ git add style.css 
$ git status
On branch testBra
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   style.css

$ 

但是豺裆,這樣有時候,并不是我們想要的号显,因為我們后續(xù)可能并不想將這個文件的修改提交留储,這時可以通過git reset命令標識沖突解決。

3.2.2 git reset標識沖突解決

首先咙轩,我們需要先將工作區(qū)復位获讳。
unstage:

$ git reset HEAD style.css 
Unstaged changes after reset:
M   style.css
$ git status
On branch testBra
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:   style.css

no changes added to commit (use "git add" and/or "git commit -a")

復位修改:

$ git checkout -- style.css 
localhost:todo_man chengxia$ git status
On branch testBra
nothing to commit, working tree clean
$ git status
On branch testBra
nothing to commit, working tree clean

重新將stash@{0}恢復:

Auto-merging style.css
CONFLICT (content): Merge conflict in style.css
$ git status
On branch testBra
Unmerged paths:
  (use "git reset HEAD <file>..." to unstage)
  (use "git add <file>..." to mark resolution)

    both modified:   style.css

no changes added to commit (use "git add" and/or "git commit -a")

再次提示沖突,這次活喊,改用git reset命令標識沖突已經(jīng)解決丐膝。

$ git reset
Unstaged changes after reset:
M   style.css
$ git status
On branch testBra
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:   style.css

no changes added to commit (use "git add" and/or "git commit -a")

注:這里在解決文件沖突的時候,沒有修改文件內(nèi)容钾菊,一般來說帅矗,我們需要將文件內(nèi)容修改為我們想要的之后(去掉標識沖突的<<<<<<>>>>>>======等標記)煞烫,再標識沖突已經(jīng)解決浑此。

4、git stash命令參考

  • (1)git stash save "save message": 執(zhí)行存儲時滞详,添加備注說明凛俱。
  • (2)git stash list:查看stash列表。
  • (3)git stash show:顯示具體做了哪些改動料饥,默認顯示第一個stash存儲蒲犬,如果要顯示其他存儲,后面加stash@{$num}岸啡,比如第二個git stash show stash@{1}原叮。
  • (4)git stash apply:應用某個存儲,但不會把存儲從存儲列表中刪除,默認使用第一個存儲,即stash@{0}巡蘸,如果要使用其他個奋隶,添加git stash apply stash@{$num},比如第二個git stash apply stash@{1}悦荒。
  • (5) git stash pop:命令恢復之前緩存的工作目錄唯欣,將緩存堆棧中的對應stash刪除,并將對應修改應用到當前的工作目錄下逾冬,默認為第一個stash,即stash@{0}黍聂,如果要應用并刪除其他stash存儲,命令:git stash pop stash@{$num}身腻。
  • (6)git stash drop stash@{$num}:刪除stash@{$num}存儲产还。
  • (7)git stash clear:刪除所有緩存的stash存儲。

新增的文件嘀趟,直接執(zhí)行stash是不會被存儲的脐区。需要先用git add命令將其添加到git暫存區(qū),才可以被git stash保存她按。

參考材料

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末牛隅,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子酌泰,更是在濱河造成了極大的恐慌媒佣,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,104評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件陵刹,死亡現(xiàn)場離奇詭異默伍,居然都是意外死亡,警方通過查閱死者的電腦和手機衰琐,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,816評論 3 399
  • 文/潘曉璐 我一進店門也糊,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人羡宙,你說我怎么就攤上這事狸剃。” “怎么了狗热?”我有些...
    開封第一講書人閱讀 168,697評論 0 360
  • 文/不壞的土叔 我叫張陵钞馁,是天一觀的道長。 經(jīng)常有香客問我匿刮,道長指攒,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,836評論 1 298
  • 正文 為了忘掉前任僻焚,我火速辦了婚禮允悦,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘虑啤。我一直安慰自己隙弛,他們只是感情好,可當我...
    茶點故事閱讀 68,851評論 6 397
  • 文/花漫 我一把揭開白布狞山。 她就那樣靜靜地躺著全闷,像睡著了一般。 火紅的嫁衣襯著肌膚如雪萍启。 梳的紋絲不亂的頭發(fā)上总珠,一...
    開封第一講書人閱讀 52,441評論 1 310
  • 那天屏鳍,我揣著相機與錄音,去河邊找鬼局服。 笑死钓瞭,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的淫奔。 我是一名探鬼主播山涡,決...
    沈念sama閱讀 40,992評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼唆迁!你這毒婦竟也來了鸭丛?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,899評論 0 276
  • 序言:老撾萬榮一對情侶失蹤唐责,失蹤者是張志新(化名)和其女友劉穎鳞溉,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體鼠哥,經(jīng)...
    沈念sama閱讀 46,457評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡穿挨,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,529評論 3 341
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了肴盏。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片科盛。...
    茶點故事閱讀 40,664評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖菜皂,靈堂內(nèi)的尸體忽然破棺而出贞绵,到底是詐尸還是另有隱情,我是刑警寧澤恍飘,帶...
    沈念sama閱讀 36,346評論 5 350
  • 正文 年R本政府宣布榨崩,位于F島的核電站,受9級特大地震影響章母,放射性物質(zhì)發(fā)生泄漏母蛛。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 42,025評論 3 334
  • 文/蒙蒙 一乳怎、第九天 我趴在偏房一處隱蔽的房頂上張望彩郊。 院中可真熱鬧,春花似錦蚪缀、人聲如沸秫逝。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,511評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽违帆。三九已至,卻和暖如春金蜀,著一層夾襖步出監(jiān)牢的瞬間刷后,已是汗流浹背的畴。 一陣腳步聲響...
    開封第一講書人閱讀 33,611評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留尝胆,地道東北人丧裁。 一個月前我還...
    沈念sama閱讀 49,081評論 3 377
  • 正文 我出身青樓,卻偏偏與公主長得像班巩,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子嘶炭,可洞房花燭夜當晚...
    茶點故事閱讀 45,675評論 2 359

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

  • 1.使用場景 發(fā)現(xiàn)有一個類是多余的抱慌,想刪掉它又擔心以后需要查看它的代碼,想保存它但又不想增加一個臟的提交 正在de...
    kevin_lln閱讀 677評論 0 0
  • 一眨猎、常用git stash命令: 1抑进、git stash 能夠?qū)⑺形刺峤坏男薷模üぷ鲄^(qū)和暫存區(qū))保存至堆棧中,用...
    宇宙小神特別萌閱讀 4,564評論 2 3
  • 一睡陪、應用場景 1寺渗、 當正在dev分支上開發(fā)某個項目,這時項目中出現(xiàn)一個bug兰迫,需要緊急修復信殊,但是正在開發(fā)的內(nèi)容只是...
    程序媛啊閱讀 1,459評論 0 1
  • 本文作者陳云峰,轉(zhuǎn)載請注明汁果。 這篇文章記錄個人常用的一些命令涡拘,和記不住的一些命令,轉(zhuǎn)載了并不斷更新据德。 Git官網(wǎng) ...
    陳云峰閱讀 2,818評論 0 24
  • git常用命令 GIT常用命令備忘:http://stormzhang.com/git/2014/01/27/gi...
    新篇章閱讀 8,492評論 1 26