您现在的位置是:首页 >学无止境 >Git 储藏(stash)详解网站首页学无止境
Git 储藏(stash)详解
此文在阅读前需要有一定的git命令基础,若基础尚未掌握,建议先阅读这篇文章Git命令播报详版
在平常的工作中,当我们在单独拉取的功能分支中进行开发时,遇到线上出现bug需要进行紧急修复,我们需要切换到主分支,进行代码的修复。但是我们直接在本地切换到主分支,则会提示当前文件的改动会丢失。
那该怎么办呢?合理的方式应该是将功能分支的代码临时存储在某个位置,当我们切回到当前分支,读取该存储,将之前的改动恢复。
在git中,就有git stash
这样的命令,完美的解决了这个问题,其将当前未提交的修改(即工作区和暂存区的修改)先暂时储藏起来。
然后我们可以通过git stash list
来查看储藏记录。之后通过git stash pop
命令将之前最近一次储藏的修改取出来,继续之前的工作,并同时将该储藏从储藏记录列表中删除
单次储藏及应用
我们在当前分支,添加test.md文件。
查看文件状态
git status
输出:
On branch master
Your branch is up to date with 'origin/master'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
test.md
nothing added to commit but untracked files present (use "git add" to track)
添加test.md到储藏区,使之能被git进行追踪
git add test.md
查看文件状态
git status
输出:
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: test.md
进行储藏
git stash
输出:Saved working directory and index state WIP on master: 709627d Initial commit
查看储藏
git stash list
输出:stash@{0}: WIP on master: 709627d Initial commit
应用最近一次储藏
git stash pop
输出:On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: test.md
Dropped refs/stash@{0} (05edd0c153191fe70ef42b9e4fdf70622d0e6831)
查看储藏
git stash list
输出为空,没有储藏记录
多次储藏及应用
多次储藏会发生什么呢?
修改文件,进行储藏,重复这一步骤,再通过git stash list
来查看这两次记录
git stash list
输出:
stash@{0}: WIP on master: 709627d Initial commit
stash@{1}: WIP on master: 709627d Initial commit
从上图结果中,我们发现两次储藏记录的标识信息完全一致,只有其前面的index
有别。
git默认按如下规则标识储藏记录(WIP意为work in progess, index
用于后面取出所对应储藏的修改)。
stash@{index}: WIP on [分支名]: [最近一次的commitID] [最近一次的提交信息]
由于我们在功能分支下的两次储藏中均未发生提交,所以其提交ID是一致的。
这样明显会带来问题,我们在多个储藏之间,无法明确需要应用哪个,需要标识下储藏记录。
可以通过命令git stash -m [stashMessage]
来标记此次储藏,以便后期查看。
通过查看储藏列表的index
的可以取出指定储藏中的修改到工作区
取出指定index
的储藏的修改到工作区中 git stash apply index
将指定index
的储藏从储藏记录列表中删除 git stash drop index
储藏记录多的话,一个个删除太麻烦,可以进行批量删除 git stash clear
修改文件,进行储藏
git stash -m "change1"
输出:Saved working directory and index state On master: change1
再次修改文件,进行储藏
git stash -m "change2"
输出:Saved working directory and index state On master: change2
查看储藏
git stash list
输出:
stash@{0}: On master: change2
stash@{1}: On master: change1
应用储藏,提交信息为"change1"
git stash apply 1
输出:
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 restore <file>..." to discard changes in working directory)
modified: README.md
no changes added to commit (use "git add" and/or "git commit -a")
查看储藏
git stash list
输出:
stash@{0}: On master: change2
stash@{1}: On master: change1
删除刚才应用的储藏
git stash drop 1
输出:Dropped refs/stash@{1} (9a6ae2acf53198fce4125161d28045c484f1b20b)
查看储藏
git stash list
输出:stash@{0}: On master: change2
再添加储藏
git stash -m "add新change"
输出:Saved working directory and index state On master: add新change
查看储藏
git stash list
输出:
stash@{0}: On master: add新change
stash@{1}: On master: change2
删除所有储藏
git stash clear
查看储藏
git stash list
输出为空,代表已经完全清除
当删除index
为0项,index
为1项中的index
会变为0。即index
会动态从0开始排序的,可看如下示例。
git stash list
输出:
stash@{0}: On master: change2
stash@{1}: On master: change1
git stash drop 0
输出:Dropped refs/stash@{0} (fb3d70dea8b439cd09cce774cf622638a341af9c)
git stash list
输出:stash@{0}: On master: change1
对特定范围文件进行储藏
默认情况下,只要在git追踪范围的文件,均可以进行储藏。我们可以进一步区分特定范围的文件进行储藏。
-
git stash [-u|--include-untracked]
:对未追踪文件也进行储藏 -
git stash [-S|--staged]
: 只对暂存区文件进行储藏 -
git stash [-a|--all]
: 对所有文件进行储藏
提前准备好三种git状态不同的文件。新添加的文件(即为未追踪文件),工作区修改的文件,提交到暂存区的文件
git status
输出:
git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: README.md
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.en.md
Untracked files:
(use "git add <file>..." to include in what will be committed)
test.md
对未追踪文件也进行储藏
git stash -u
输出:Saved working directory and index state WIP on master: 709627d Initial commit
查看文件状态
git status
输出:
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
对刚才储藏进行应用
git stash pop
输出:
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 restore <file>..." to discard changes in working directory)
modified: README.en.md
modified: README.md
Untracked files:
(use "git add <file>..." to include in what will be committed)
test.md
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (23b4e04af9cc34612f458648e838f552a1f99169)
将README.md添加到暂存区
git add README.md
查看文件状态
git status
输出:
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: README.md
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.en.md
Untracked files:
(use "git add <file>..." to include in what will be committed)
test.md
只对暂存区文件进行储藏
git stash -S
输出:Saved working directory and index state WIP on master: 709627d Initial commit
查看文件状态
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 restore <file>..." to discard changes in working directory)
modified: README.en.md
Untracked files:
(use "git add <file>..." to include in what will be committed)
test.md
no changes added to commit (use "git add" and/or "git commit -a")
重复之前步骤。将储藏进行应用,同时将README.md添加到暂存区
全部文件添加到储藏区
git stash -a
输出:Saved working directory and index state WIP on master: 709627d Initial commit
查看文件状态
git status
输出:
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
若想对stash
命令了解更多,可以通过命令git stash --help
查阅。