git stash -help我们可以看到stash相关的命令,下面我们分别介绍一下它们
usage: git stash list [<options>]
or: git stash show [<options>] [<stash>]
or: git stash drop [-q|--quiet] [<stash>]
or: git stash ( pop | apply ) [--index] [-q|--quiet] [<stash>]
or: git stash branch <branchname> [<stash>]
or: git stash clear
or: git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]
[-u|--include-untracked] [-a|--all] [-m|--message <message>]
[--pathspec-from-file=<file> [--pathspec-file-nul]]
[--] [<pathspec>...]]
or: git stash save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]
[-u|--include-untracked] [-a|--all] [<message>]
1、git stash
? [/Users/test/Desktop/ruby] git:(master) vi feature_test_file1
? [/Users/test/Desktop/ruby] git:(master) ? git st
On branch 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: feature_test_file1
no changes added to commit (use "git add" and/or "git commit -a")
进行多次修改并satsh
? [/Users/test/Desktop/ruby] git:(master) ? git stash save "第一次修改stash"
Saved working directory and index state On master: 第一次修改stash
? [/Users/test/Desktop/ruby] git:(master) vi feature_test_file1
? [/Users/test/Desktop/ruby] git:(master) ? git stash save "第二次修改stash"
Saved working directory and index state On master: 第二次修改stash
? [/Users/test/Desktop/ruby] git:(master) vi feature_test_file1
? [/Users/test/Desktop/ruby] git:(master) ? git stash save "第三次修改stash"
Saved working directory and index state On master: 第三次修改stash
在每一次stash的时候,我们使用git stash save "message" ,之后我们利用git stash list 查看目前暂存区的状态。
? [/Users/test/Desktop/ruby] git:(master) git stash list
stash@{0}: On master: 第三次修改stash
stash@{1}: On master: 第二次修改stash
stash@{2}: On master: 第一次修改stash
(2)新建文件之后使用stash时,不会stash文件
原因是新建的文件没有在git版本控制,所以不能stash,可以使用两种方式stash
- 需要先执行git add该文件将其加入到版本控制中,在git stash
? [/Users/test/Desktop/ruby] git:(master) touch feature_test_file4
? [/Users/test/Desktop/ruby] git:(master) ? git st
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
feature_test_file4
nothing added to commit but untracked files present (use "git add" to track)
? [/Users/test/Desktop/ruby] git:(master) ? git stash
No local changes to save
? [/Users/test/Desktop/ruby] git:(master) ? git st
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
feature_test_file4
nothing added to commit but untracked files present (use "git add" to track)
? [/Users/test/Desktop/ruby] git:(master) ? git st
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
feature_test_file4
# 需要先执行git add该文件将其加入到版本控制中,在git stash
? [/Users/test/Desktop/ruby] git:(master) ? git add .
? [/Users/test/Desktop/ruby] git:(master) ? git stash
Saved working directory and index state WIP on master: 5598714 init
? [/Users/test/Desktop/ruby] git:(master) git st
On branch master
nothing to commit, working tree clean
(3) git stash save -u “message”
? [/Users/test/Desktop/ruby] git:(master) git stash apply stash@{0}
Already up to date!
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
feature_test_file5
nothing added to commit but untracked files present (use "git add" to track)
? [/Users/test/Desktop/ruby] git:(master) ? vi feature_test_file5
? [/Users/test/Desktop/ruby] git:(master) ? git stash save -u "jj"
Saved working directory and index state On master: jj
? [/Users/test/Desktop/ruby] git:(master) git stash list
? [/Users/test/Desktop/ruby] git:(master)
stash@{0}: On master: jj
stash@{1}: WIP on master: 5598714 init
stash@{2}: WIP on master: 5598714 init
stash@{3}: WIP on master: 5598714 init
stash@{4}: On master: 第一次修改stash
? [/Users/test/Desktop/ruby] git:(master) vi feature_test_file5
? [/Users/test/Desktop/ruby] git:(master) ? git st
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
feature_test_file5
nothing added to commit but untracked files present (use "git add" to track)
? [/Users/test/Desktop/ruby] git:(master) ? git stash save -u "hh"
stash@{0}: On master: hh
stash@{1}: On master: jj
stash@{2}: WIP on master: 5598714 init
stash@{3}: WIP on master: 5598714 init
stash@{4}: WIP on master: 5598714 init
stash@{5}: On master: 第一次修改stash
- 指定
--include-untracked 或 -u 选项,Git 也会贮藏任何未跟踪文件。
在贮藏中包含未跟踪的文件仍然不会包含明确 忽略 的文件。 要额外包含忽略的文件,请使用 --all 或 -a 选项。
? [/Users/test/Desktop/ruby] git:(master) touch feature_test_file5
? [/Users/test/Desktop/ruby] git:(master) ? git st
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
feature_test_file5
nothing added to commit but untracked files present (use "git add" to track)
? [/Users/test/Desktop/ruby] git:(master) ? git stash -u
Saved working directory and index state WIP on master: 5598714 init
? [/Users/test/Desktop/ruby] git:(master) git st
On branch master
nothing to commit, working tree clean
? [/Users/test/Desktop/ruby] git:(master) git stash list
stash@{2}: WIP on master: 5598714 init
stash@{3}: WIP on master: 5598714 init
stash@{4}: On master: 第二次修改stash
stash@{5}: On master: 第一次修改stash
#但是修改过未追踪的文件之后,git st显示的信息还是untrack
? [/Users/test/Desktop/ruby] git:(master) vi feature_test_file5
? [/Users/test/Desktop/ruby] git:(master) ? git st
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
feature_test_file5
nothing added to commit but untracked files present (use "git add" to track)
2、查看stash内容
-
git stash list: 查看stash列表 -
git stash show: 查看第一个储藏做了哪些改动,不是改动详情 -
git stash show stash@{num}: 查看其他储藏做了哪些改动 -
git stash show -p: 查看第一个储藏的改动 -
git stash show -p stash@{num}: 查看其他储藏做的改动
? [/Users/test/Desktop/ruby] git:(master) git stash show stash@{1}
feature_test_file1 | 1 +
1 file changed, 1 insertion(+)
? [/Users/test/Desktop/ruby] git:(master) git stash show -p stash@{1}
diff --git a/feature_test_file1 b/feature_test_file1
index e69de29..dd98f1d 100644
--- a/feature_test_file1
+++ b/feature_test_file1
@@ -0,0 +1 @@
+第二次修改
3、应用stash内容
- git stash apply: 应用第一个储藏,但不会删除
- git stash apply stash@{num}: 应用列表中的储藏
- git stash pop: 应用第一个储藏并删除
- git stash pop stash@{num}: 应用stash list中的第num个储藏并删除
(1)下面我们通过实例看一下git stash apply
? [/Users/test/Desktop/ruby] git:(master) git stash apply stash@{1}
On branch 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: feature_test_file1
no changes added to commit (use "git add" and/or "git commit -a")
#可以看到git stash apply stash@{num}应用了stash,但是没有删除对应的stash
? [/Users/test/Desktop/ruby] git:(master) git stash list
stash@{0}: On master: 第三次修改stash
stash@{1}: On master: 第二次修改stash
stash@{2}: On master: 第一次修改stash
(2)下面我们通过实例看一下git stash pop
? [/Users/test/Desktop/ruby] git:(master) git stash pop stash@{0}
On branch 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: feature_test_file1
no changes added to commit (use "git add" and/or "git commit -a")
Dropped stash@{0} (2ce69a010ebfc53b0ba1619c509a9ae0b52d2861)
? [/Users/test/Desktop/ruby] git:(master) ? vi feature_test_file1
第三次修改
? [/Users/test/Desktop/ruby] git:(master) git stash list
stash@{1}: On master: 第二次修改stash
stash@{2}: On master: 第一次修改stash
(3)其他的小实验
- 如果我们应用了一个stash之后未对修改保存,在应用其他stash会报错
? [/Users/test/Desktop/ruby] git:(master) ? git st
On branch 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: feature_test_file1
no changes added to commit (use "git add" and/or "git commit -a")
? [/Users/test/Desktop/ruby] git:(master) ? vi feature_test_file1
? [/Users/test/Desktop/ruby] git:(master) ? git stash apply stash@{0}
error: Your local changes to the following files would be overwritten by merge:
feature_test_file1
Please commit your changes or stash them before you merge.
Aborting
最佳实践:应用一个stash之后确认是否使用,如果使用形成commit,如果不使用restore
4、删除stash内容
? [/Users/test/Desktop/ruby] git:(master) git stash list
#这一条stash未使用stash save
stash@{0}: WIP on master: 5598714 init
stash@{1}: On master: 第二次修改stash
stash@{2}: On master: 第一次修改stash
? [/Users/test/Desktop/ruby] git:(master) git stash drop
Dropped refs/stash@{0} (75aa0952a817c3d753674007983c1c792a8bd9c9)
? [/Users/test/Desktop/ruby] git:(master) git stash list
stash@{0}: On master: 第二次修改stash
stash@{1}: On master: 第一次修改stash
5、从stash创建分支
git stash branch 分支名字stash@{0}
? [/Users/test/Desktop/ruby] git:(master) git stash branch feature_from_stash stash@{0}
Switched to a new branch 'feature_from_stash'
Already up to date!
On branch feature_from_stash
Untracked files:
(use "git add <file>..." to include in what will be committed)
feature_test_file5
nothing added to commit but untracked files present (use "git add" to track)
Dropped stash@{0} (f257c0cd558430e0550048149bbbb9a4be0eafaf)
? [/Users/test/Desktop/ruby] git:(master) git log
commit 5598714bc98500e627b82bd1903d35c88d2b8e8f (HEAD -> feature_from_stash, master)
Author: test <test@staff.weibo.com>
Date: Mon Sep 19 10:05:28 2022 +0800
init
我们在master分支上最后的修改是在文件5里面输入了“第二次修改”并且保存了,我们看看文件5发现确实是该内容
第二次修改
我们看一下该分支有哪些stash
? [/Users/test/Desktop/ruby] git:(feature_from_stash) ? git stash list
stash@{0}: On master: jj
stash@{1}: WIP on master: 5598714 init
stash@{2}: WIP on master: 5598714 init
stash@{3}: WIP on master: 5598714 init
stash@{4}: On master: 第一次修改stash
我们切到master分支看一下stash,发现两个分支的stash状态一样
? [/Users/test/Desktop/ruby] git:(master) git stash list
stash@{0}: On master: jj
stash@{1}: WIP on master: 5598714 init
stash@{2}: WIP on master: 5598714 init
stash@{3}: WIP on master: 5598714 init
stash@{4}: On master: 第一次修改stash
下面我们再看一个在git管理的文件的相关分支的操作
? [/Users/test/Desktop/ruby] git:(feature_from_stash4) ? git stash list
stash@{0}: On master: master上修改file1
stash@{1}: On feature_from_stash3: stash3分支stash
stash@{2}: On feature_from_stash2: stash2分支stash
stash@{3}: WIP on master: 5598714 init
stash@{4}: WIP on master: 5598714 init
stash@{5}: On master: 第一次修改stash
? [/Users/test/Desktop/ruby] git:(master) git stash branch feature_from_stash4 stash@{0}
Switched to a new branch 'feature_from_stash4'
On branch feature_from_stash4
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: feature_test_file1
no changes added to commit (use "git add" and/or "git commit -a")
Dropped stash@{0} (5bdf08d31455b2a3a6ede4647cec8a61d005f22f)
? [/Users/test/Desktop/ruby] git:(feature_from_stash4) ? git stash list
stash@{0}: On feature_from_stash3: stash3分支stash
stash@{1}: On feature_from_stash2: stash2分支stash
stash@{2}: WIP on master: 5598714 init
stash@{3}: WIP on master: 5598714 init
stash@{4}: On master: 第一次修改stash
|