当前位置:   article > 正文

git stash超详解,看本文就够了_git中的stash

git中的stash

1. stash工具的应用场景

当本地修改了一批代码,但是需要提交的只是修改中的部分文件或部分代码,可以将不需要提交的部分存入stash,push完之后pop出来继续开发。

2. stash常用命令

1) git stash

作用:将所有修改的文件添加到stash

$ git status
On branch gxtdev
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:   file1.c
        modified:   file2.c
        modified:   file3.c

no changes added to commit (use "git add" and/or "git commit -a")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
$ git stash
Saved working directory and index state WIP on gxtdev: 216a546 Merge branch 'master' into gxtdev
  • 1
  • 2

另外,git stash save “xxx” 表示将修改的文件加入缓存且以xxx的名字保存,与git stash功能一致,只不过增加添加备注的功能。

$ git stash save "test_drv"
Saved working directory and index state On master: test_drv
  • 1
  • 2

2) git stash list

作用:查看stash列表,列表以节点的形式存在

$ git stash list
stash@{0}: WIP on gxtdev: 216a546 Merge branch 'master' into gxtdev
stash@{1}: WIP on gxtdev: 7cb4301 删除xxxxxx
  • 1
  • 2
  • 3

3) git stash show

作用:查看stash中文件的更改情况(哪个文件有几处修改)

$ git stash show
 file1.c 			| 2 +-
 file2.c 			| 2 +-
 file3.c  			| 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
  • 1
  • 2
  • 3
  • 4
  • 5

使用git stash show stash@{xx}可查看某一个stash缓存的改动情况。

$ git stash show stash@{1}
 file1.c    		|   9 ++
 file2.c          	|  60 ++++++++-
 file3.c      		|  69 +++++-----
 file4.c 			| 236 ++++++++++++++++-----------------
 4 files changed, 220 insertions(+), 154 deletions(-)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

4) git stash pop

作用:将stash中的文件"弹"出来

$ git stash pop
On branch gxtdev
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:   file1.c
        modified:   file2.c
        modified:   file3.c

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (8497606f0036fbb5f13e16b0f13ad489aexxxx)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

如果想弹出指定的stash,则执行git stash pop stash@{xx},如:

$ git stash pop stash@{0}
  • 1

5) git stash apply

作用:将stash中的文件"弹"到当前分支,但不会在stash栈中删除该分支,应用在将此次改动在多个分支中应用的情况

$ git stash apply
$ git stash apply stash@{xx}
  • 1
  • 2

6) git stash clear

作用:清除stash

$ git stash clear
无提示即clear成功
$ git stash list
无提示即stash中无内容
  • 1
  • 2
  • 3
  • 4

7) git stash drop

作用:清除stash,相比git stash clear的优点是可以删除指定的stash缓存

$ git stash drop stash@{0}
Dropped stash@{0} (057a7959cd8b571965e644f7552e7543967ae187)
  • 1
  • 2

8) git stash push [file1] [file2] [file3]…

作用:将部分文件添加到stash

该指令非常实用,不像第1条将所有的文件添加到stash,该语句可以自行选择加入stash的文件

9) git stash -p

作用:该指令是一个交互式命令,能够一个文件一个文件的遍历,决定每一个文件的操做方式。y放入stash,n不放入stash

该指令对于文件中的每一处修改都进行操作算则是否加入stash,个人觉得该指令针对同一个文件中同时存在有push和stash的情况比较使用。

分享完毕~

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号