Git -- Stash

本文主要介绍stash操作,常用于储藏未完成的工作,以免将脏状态保存为一个提交

git stash

git stash:储藏working directoryindex的当前状态(不包括untracked文件)
git stash apply:仅应用working directory的修改,不应用index的修改
git stash list:stash列表
git stash drop:删除stash

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
$ gst -sb
## master
M a.txt
M b.txt

$ git stash list # Stdout print nothing

$ git stash # save working directory and index state
Saved working directory and index state WIP on master: 2e5960b add a.txt b.txt
HEAD is now at 2e5960b add a.txt b.txt

$ gst
On branch master
nothing to commit, working directory clean

$ git checkout -b dev

$ git stash list
stash@{0}: WIP on master: 2e5960b add a.txt b.txt

$ git stash apply # default apply stash@{0}

$ gst -sb # only apply working directory
## dev
M a.txt
M b.txt

$ git stash drop stash@{0}
Dropped stash@{0} (8ec72e0160fd187bcc90ddcc7066b9b6c22f350c)

$ git stash list # Stdout print nothing

git stash apply --index

git stash apply --index:应用working directoryindex的修改

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$ gst -sb
## dev
M a.txt
M b.txt

$ git stash
Saved working directory and index state WIP on dev: 2e5960b add a.txt b.txt
HEAD is now at 2e5960b add a.txt b.txt

$ gst
On branch dev
nothing to commit, working directory clean

$ git stash apply --index # apply working directory and index

$ gst -sb
## dev
M a.txt
M b.txt

git stash --keep-index

git stash --keep-index:仅储藏working directory的修改,不储藏index的修改

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$ gst -sb
## dev
M a.txt
M b.txt

$ git stash --keep-index
Saved working directory and index state WIP on dev: 2e5960b add a.txt b.txt
HEAD is now at 2e5960b add a.txt b.txt

$ gst -sb
## dev
M a.txt

$ git stash apply

$ gst -sb
## dev
M a.txt
M b.txt

git stash -u

git stash -u:储藏untracked文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
$ gst -sb
## dev
M a.txt
M b.txt
?? c.txt

$ git stash
Saved working directory and index state WIP on dev: 2e5960b add a.txt b.txt
HEAD is now at 2e5960b add a.txt b.txt

$ gst -sb
## dev
?? c.txt

$ git stash apply

$ gst -sb
## dev
M a.txt
M b.txt
?? c.txt

$ git stash -u
Saved working directory and index state WIP on dev: 2e5960b add a.txt b.txt
HEAD is now at 2e5960b add a.txt b.txt

$ gst
On branch dev
nothing to commit, working directory clean
0%