当前位置:   article > 正文

Git 储藏(stash)详解_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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

添加test.md到储藏区,使之能被git进行追踪

git add test.md
  • 1

查看文件状态

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

进行储藏

git stash

输出:Saved working directory and index state WIP on master: 709627d Initial commit
  • 1
  • 2
  • 3

查看储藏

git stash list

输出:stash@{0}: WIP on master: 709627d Initial commit
  • 1
  • 2
  • 3

应用最近一次储藏

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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

查看储藏

git stash list

输出为空,没有储藏记录
  • 1
  • 2
  • 3

多次储藏及应用

多次储藏会发生什么呢?

修改文件,进行储藏,重复这一步骤,再通过git stash list来查看这两次记录

git stash list

输出:
stash@{0}: WIP on master: 709627d Initial commit
stash@{1}: WIP on master: 709627d Initial commit
  • 1
  • 2
  • 3
  • 4
  • 5

从上图结果中,我们发现两次储藏记录的标识信息完全一致,只有其前面的index有别。

git默认按如下规则标识储藏记录(WIP意为work in progess, index用于后面取出所对应储藏的修改)。

stash@{index}: WIP on [分支名]: [最近一次的commitID] [最近一次的提交信息]
  • 1

由于我们在功能分支下的两次储藏中均未发生提交,所以其提交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
  • 1
  • 2
  • 3

再次修改文件,进行储藏

git stash -m "change2" 

输出:Saved working directory and index state On master: change2
  • 1
  • 2
  • 3

查看储藏

git stash list

输出:
stash@{0}: On master: change2
stash@{1}: On master: change1
  • 1
  • 2
  • 3
  • 4
  • 5

应用储藏,提交信息为"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")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

查看储藏

git stash list

输出:
stash@{0}: On master: change2
stash@{1}: On master: change1
  • 1
  • 2
  • 3
  • 4
  • 5

删除刚才应用的储藏

git stash drop 1

输出:Dropped refs/stash@{1} (9a6ae2acf53198fce4125161d28045c484f1b20b)
  • 1
  • 2
  • 3

查看储藏

git stash list

输出:stash@{0}: On master: change2
  • 1
  • 2
  • 3

再添加储藏

git stash -m "add新change"  

输出:Saved working directory and index state On master: add新change
  • 1
  • 2
  • 3

查看储藏

git stash list

输出:
stash@{0}: On master: add新change
stash@{1}: On master: change2
  • 1
  • 2
  • 3
  • 4
  • 5

删除所有储藏

git stash clear
  • 1

查看储藏

git stash list
  • 1

输出为空,代表已经完全清除

当删除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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

对特定范围文件进行储藏

默认情况下,只要在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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

对未追踪文件也进行储藏

git stash -u

输出:Saved working directory and index state WIP on master: 709627d Initial commit
  • 1
  • 2
  • 3

查看文件状态

git status

输出:
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

对刚才储藏进行应用

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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

将README.md添加到暂存区

git add README.md 
  • 1

查看文件状态

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

只对暂存区文件进行储藏

git stash -S
输出:Saved working directory and index state WIP on master: 709627d Initial commit
  • 1
  • 2

查看文件状态

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")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

重复之前步骤。将储藏进行应用,同时将README.md添加到暂存区

全部文件添加到储藏区

git stash -a
输出:Saved working directory and index state WIP on master: 709627d Initial commit
  • 1
  • 2

查看文件状态

git status

输出:
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

若想对stash命令了解更多,可以通过命令git stash --help查阅。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/273681
推荐阅读
相关标签
  

闽ICP备14008679号