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
這時鬓照,提示沖突如下:
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
保存她按。