下載初始化倉(cāng)庫(kù)
$ git status
當(dāng)前位于test分支旋恼,并且修改啦兩個(gè)文件,一個(gè)已經(jīng)staged,還有一個(gè)沒有
$ git stash
stash命令把當(dāng)前`working directory`和`index`的內(nèi)容都存儲(chǔ)起來(lái)
$ git status
現(xiàn)在status命令查看蛾魄,`working directory`已經(jīng)沒有修改啦数尿,是干凈的蜜另。
$ git stash list
`stash list`命令就是列出已經(jīng)stash的內(nèi)容,輸出結(jié)果還包括啦stash時(shí)的分支和當(dāng)時(shí)所在commit的SHA-1值
如果有新的stash時(shí)货邓,會(huì)在老的列表上面新建,把現(xiàn)在的這個(gè)stash@{0}壓下去四濒,變成stash@{1}
$ git log --oneline --graph --decorate
分支結(jié)構(gòu)圖可知换况,`stash list`輸出結(jié)果中的確實(shí)是stash時(shí)所在分支所指向的commit的SHA-1值
$ git checkout master
切換到`master`分支
打開hello.txt职辨,作如上的修改
$ git stash apply
$ git stash apply stash@{0}
恢復(fù)stash的內(nèi)容,用上面的命令就行戈二,可以在apply后面聲明是哪一個(gè)stash舒裤,
不聲明,Git會(huì)默認(rèn)選擇最新的那個(gè)觉吭。
恢復(fù)stash并不一定要保持`working directory`干凈腾供,
也不一定要在同一個(gè)branch上(在test的stash可以恢復(fù)到master分支),
如果恢復(fù)時(shí)出現(xiàn)沖突鲜滩,git停止恢復(fù)伴鳖,提示沖突。
因?yàn)閟tash存儲(chǔ)啦對(duì)hello的修改徙硅,所以恢復(fù)時(shí)提示沖突黎侈,停止恢復(fù),
提示我們要先commit或者stash才可以繼續(xù)恢復(fù)
但是闷游,如果修改的是afternoon.txt文件(stash存儲(chǔ)中沒有對(duì)這個(gè)文件的修改)峻汉,
那么可以直接`git stash apply`完成恢復(fù),不會(huì)產(chǎn)生沖突
$ git add hello.txt
$ git stash apply
因?yàn)閟tash apply命令是把原來(lái)存儲(chǔ)的修改都恢復(fù)到`working dirctory`脐往,
所以可以先把當(dāng)前對(duì)hello.txt的修改添加到`Index`休吠,
然后在stash apply恢復(fù)
$ git checkout -- hello.txt morning.txt
$ git status
用checkout命令,把`working dirctory`中剛剛恢復(fù)的修改撤銷掉业簿,然后再查看status瘤礁,只有index里面還有hello的修改
$ git stash apply --index
這個(gè)命令恢復(fù)stash的內(nèi)容時(shí),
會(huì)把原來(lái)已經(jīng)staged的文件(就是在index中的文件)也恢復(fù)梅尤,
不加`--index`時(shí)柜思,會(huì)把所有存儲(chǔ)的操作都恢復(fù)到`working directory`,
因?yàn)閕ndex中有對(duì)hello的修改存在巷燥,所以恢復(fù)依然終止
$ git status
查看status赡盘,stash沒有恢復(fù),原來(lái)index中的修改也清楚啦缰揪,`working directory`的修改保留
$ git checkout -- hello.txt
$ git stash apply stash@{0} --index
撤銷`working directory`的hello.txt修改陨享,然后用加上`--index`的命令恢復(fù)stash,此時(shí)輸出結(jié)果可以看到钝腺,跟剛開始在test分支時(shí)的狀態(tài)是一樣的抛姑,修改的hello.txt添加到index,morning.txt的修改沒有添加
$ git stash list
$ git stash drop stash@{0}
用drop stash命令丟棄無(wú)用的stash
$ git status
$ git stash
此時(shí)master分支仍然保留著恢復(fù)時(shí)的狀態(tài)艳狐,再次用git stash存儲(chǔ)狀態(tài)
$ git stash list
$ git stash pop stash@{0}
$ git stash list
用`git stash pop`命令恢復(fù)stash后會(huì)立即從棧中刪除對(duì)應(yīng)的stash
`git stash list`沒有輸出結(jié)果
Stash命令的一些其他參數(shù)的使用
$ git add hello.txt
$ git status -s
把hello.txt添加到index定硝,然后再?gòu)?fù)制一份hello.txt,名字是`hello (2).txt`
用git status -s查看狀態(tài)毫目,如上圖
$ git stash --keep-index --include-untracked
$ git stash --keep-index -u //-u == --include-untracked
$ git status -s
參數(shù)`--keep-index`表示保留index里面的狀態(tài)
`--include-untracked`表示包括沒有追蹤的文件(就是上面復(fù)制的hello (2).txt)
然后在用`status -s`查看蔬啡,只剩下index中的狀態(tài)啦
$ git checkout --f master
$ git status -s
`checkout --f master`撤銷掉所有更改诲侮,打開hello.txt文件,前面兩行添加(master1)(master2)最后兩行加上(master19)和(master20)
$ git stash --patch
y
n
`--patch`參數(shù)用于存儲(chǔ)部分的修改星爪,如圖浆西,對(duì)前面兩個(gè)修改進(jìn)行存儲(chǔ),不存儲(chǔ)后面兩個(gè)修改
打開hello.txt顽腾,可以看到近零,上面兩行的修改已經(jīng)存儲(chǔ)起來(lái)了,下面的兩行依然保留
如果stash已經(jīng)無(wú)法在當(dāng)前分支上恢復(fù)抄肖,可以嘗試從stash新建一個(gè)branch測(cè)試代碼
$ git checkout -f HEAD //撤銷掉上面剩下的所有修改
$ git stash branch testchanges stash@{0}
`stash branch testchanges`命令將新建testchanges分支久信,checkout執(zhí)行stash存儲(chǔ)命令時(shí)所在的那個(gè)commit,然后把存儲(chǔ)的修改重新引用到那個(gè)commit上漓摩,testchanges分支指向那個(gè)commit