贮藏(stash)可以做什么?
- 贮藏可以隐藏工作区与暂存区的变化(新增、修改、删除),使工作区与暂存区就像当前分支刚checkout一样干净
- 可以从贮藏区恢复之前保存的工作,或是从贮藏区创建新的分支,继续后面的工作
在学习贮藏工作之前,需要先了解什么是已跟踪文件和未跟踪文件。我们知道git有分工作区、暂存区、版本库,所有新创建的文件或修改的文件都会在工作区。那么在工作区中新创建的文件就是未跟踪文件,非工作区中新创建的文件(包括工作区与暂存区中的文件)为已跟踪文件。
如何运用贮藏
下面首先我们通过git status看一下工作区和暂存区,工作区中有一个新创建的文件newFile.txt和一个已修改文件README.md,暂存区中有一个文件file1.txt。
$ git status
On branch feature/xujingjian-xjV4-4-1-1.3.0
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: file1.txt
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: README.md
Untracked files:
(use "git add <file>..." to include in what will be committed)
newFile.txt
现在我们来执行贮藏git stash 或 git stash push
$ git stash
Saved working directory and index state WIP on xujingjian-xjV4-4-1-1.3.0: e76ea876 bug 99500
$ git status
On branch feature/xujingjian-xjV4-4-1-1.3.0
Untracked files:
(use "git add <file>..." to include in what will be committed)
newFile.txt
nothing added to commit but untracked files present (use "git add" to track)
我们可以看到一个已修改文件README.md,暂存区中有一个文件file1.txt已经不见了。
有人会想问为什么newFile.txt还在呢?因为只是执行git stash 或 git stash push不会贮藏未跟踪的文件。如果要贮藏未跟踪的文件,需要加上参数--include-untracked
或 -u。
$ git stash -u
Saved working directory and index state WIP on xujingjian-xjV4-4-1-1.3.0: e76ea876 bug 99500
$ git status
On branch feature/xujingjian-xjV4-4-1-1.3.0
nothing to commit, working tree clean
现在newFile.txt也不见了。现在工作区和暂存区都干净了,你可以开始新的工作。
如果希望贮藏同时保留暂存区应该如何?加上参数--keep-index
$ git stash --keep-index
Saved working directory and index state WIP on xjV4-4-1-1.3.0: 21b44945
$ git status
On branch xjV4-4-1-1.3.0
Your branch is up to date with 'origin/province/xinjiang/V4-4-1(1-3-0锛?.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: file1.txt
可以看到file1.txt,虽然已经贮藏,但还在暂存区中。
贮藏的都去哪里了呢?执行git stash list查看
$ git stash list
stash@{0}: WIP on xujingjian-xjV4-4-1-1.3.0: e76ea876 bug 99500
stash@{1}: WIP on xujingjian-xjV4-4-1-1.3.0: e76ea876 bug 99500
现在我们来看如何恢复?
如果只恢复上一次的贮藏,可以执行git stash apply。如果要想恢复其他的贮藏,可以指定贮藏名字,如执行git stash apply stash@{2}
$ git stash apply
Already up to date!
On branch feature/xujingjian-xjV4-4-1-1.3.0
Untracked files:
(use "git add <file>..." to include in what will be committed)
newFile.txt
nothing added to commit but untracked files present (use "git add" to track)
如何删除贮藏?删除跟恢复类似,可以执行git stash drop。如果要想删除其他的贮藏,可以指定贮藏名字,如执行git stash drop stash@{2}。
如果要也恢复之后同时删除,可以运行git stash pop,用法跟git stash apply一样。
注意:如果恢复的时候,文件有冲突,跟合并一样要解决冲突。
从贮藏创建一个分支
通过git stash branch <新分支>,下面我们来演示,把之前贮藏如何恢复到新分支。
$ git stash branch testchanges
Switched to a new branch 'testchanges'
$ git status
On branch testchanges
Untracked files:
(use "git add <file>..." to include in what will be committed)
newFile.txt
nothing added to commit but untracked files present (use "git add" to track)
$ git stash apply stash@{1}
On branch testchanges
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: file1.txt
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: README.md
Untracked files:
(use "git add <file>..." to include in what will be committed)
newFile.txt
我们通过git stash branch testchanges,创建testchanges分支,同时恢复了最后一次贮藏stash@{0}
再通过git stash apply stash@{1},恢复了上一次贮藏stash@{1}。
我们可以看到工作区与暂存区已经有了跟前面贮藏时完全一样的文件。