Git实用教程-第十一篇:Git工具之储藏(git stash)

       我们在软件开发中,往往一边在开发新功能,一边要面临着紧急修复线上bug。前面我们说过,这时需要新建一个临时bug分支来修复,但切换分支前,需要将当前正在开发的这部分代码保存起来,但开发到一半,我不想提交到本地版本库,有没有什么办法将这部分代码先隐藏,然后切换分支去修复bug,等bug修复完成后,再回到当前分支,将之前隐藏的代码拿出来继续开发呢?幸好,Git已经为我们提供了这样的工具:。

一、作用

git stash 可以将当前工作区和暂存区所有未提交的改动保存起来,存储在单独开辟的一个独立空间中,如果存储了多个stash,是先进后出,可以理解为缓存堆栈。任何时候、任何分支都可以将stash弹出来。

二、基本操作

1、stash存储

首先切换到test分支,编辑文件readme.txt,添加一行记录:111,并添加至暂存区

$ vi readme.txt
Git is a distributed version control system.
Git is open source and free. Git is easy to learn.
Hello Git.
111

$ git add readme.txt

然后再编辑文件readme.txt,再添加一行记录:222,不添加至暂存区

$ vi readme.txt
Git is a distributed version control system.
Git is open source and free. Git is easy to learn.
Hello Git.
111
222

查看当前状态

$ git status
On branch test
Changes to be committed:
  (use "git reset HEAD ..." to unstage)

    modified:   readme.txt

Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git checkout -- ..." to discard changes in working directory)

    modified:   readme.txt

目前,暂存区添加的一行是111,工作区添加的一行是222

这时,接到需求,要回到master分支,修改bug,可以使用git stash(git stash是git stash save的缩写)命令将这些改动存储起来

$ git stash
Saved working directory and index state WIP on test: 7977066 Merge branch 'dev' into test

$ git status
On branch test
nothing to commit, working tree clean

保存成功,可以发现当前工作区的内容已经还原到本地最新的commit版本,切换到master分支,添加一行记录"Small and Fast."

$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.

$ vi readme.txt
Git is a distributed version control system.
Git is open source and free. Git is easy to learn.
Hello Git.
Small and Fast.
$ git add readme.txt
$ git commit -m "small and fast"
[master a4cd6b0] small and fast
 1 file changed, 1 insertion(+)

bug修复完成后,切换到test分支,取回之前未完成的工作,继续工作。
git stast list命令可以查看stash内容列表

$ git checkout test
Switched to branch 'test'

$ git stash list
stash@{0}: WIP on test: 7977066 Merge branch 'dev' into test

2、stash弹出并删除

命令,可以从缓存堆栈中弹出最新的的stash内容,同时缓存堆栈中也会删除该stash内容。

$ git stash pop
On branch test
Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git checkout -- ..." to discard changes in working directory)

    modified:   readme.txt

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (65807853e29606b50e035952b38a30103722abfb)

输出的内容显示,已经从stash缓存堆栈中恢复了之前存储的内容,并删除了该stash。可以通过git stash list查看,发现此时已经没有任何内容了。

$ git stash list

3、stash弹出并保留

如果我们从stash缓存堆栈中恢复存储的内容时,不想删除该stash,该怎么办呢?可以使用命令

$ git stash
Saved working directory and index state WIP on test: 7977066 Merge branch 'dev' into test

$ git stash apply
On branch test
Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git checkout -- ..." to discard changes in working directory)

    modified:   readme.txt

no changes added to commit (use "git add" and/or "git commit -a")

$ git stash list
stash@{0}: WIP on test: 7977066 Merge branch 'dev' into test

4、stash删除

如果想直接删除stash缓存堆栈中最新的stash内容,可以使用命令

$ git stash list
stash@{0}: WIP on test: 7977066 Merge branch 'dev' into test

$ git stash drop
Dropped refs/stash@{0} (5aebb0f541ab919963d8784e1f95fe070f4c51d3)

$ git stash list

5、stash未被追踪的文件

刚刚我们通过stash存储的都是已经被Git追踪过的文件,如果这时有个新文件,直接使用git stash命令是不能存储的,需要添加参数 -u

$ touch temp.txt 
 ​
$ git stash -u
Saved working directory and index state WIP on test: 7977066 Merge branch 'dev' into test

$ git status
On branch test
nothing to commit, working tree clean

6、查看stash的详细内容

时间一长,可能已经忘记存储在stash中的具体内容了,可以通过git stash show 命令来查看详情

$ git stash list
stash@{0}: WIP on test: 7977066 Merge branch 'dev' into test

$ git stash show stash@{0}
 readme.txt | 2 ++
 1 file changed, 2 insertions(+)

加上 -p 参数可以查看具体内容

$ git stash show stash@{0} -p
diff --git a/readme.txt b/readme.txt
index 4b3e672..51bea68 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,3 +1,5 @@
 Git is a distributed version control system.
 Git is open source and free. Git is easy to learn.
 Hello Git.
+111
+222

小结

1、stash所有未提交的文件

$ git stash -u

2、stash被追踪过的未提交的文件

$ git stash

3、查看stash列表

$ git stash list

4、从stash缓存堆栈中弹出最新的stash,并删除该stash

$ git stash pop

5、从stash缓存堆栈中弹出最新的stash,并保留该stash

$ git stash apply

6、删除stash缓存堆栈中最新的一个stash

$ git stash drop

7、查看某个stash的详情

$ git stash show  -p

你可能感兴趣的:(Git实用教程-第十一篇:Git工具之储藏(git stash))