场景

本地正在修改代码,需要切换其他分支或者新的工作区,但是不想提交,也不想新建分支,那么就可以使用git stash命令用作临时暂存,先去干其他事,后续随时恢复。

常用命令

1
2
3
4
5
6
7
8
9
10
11
git stash
git stash save
git stash -u
git stash -a
git stash list
git stash apply
git stash pop
git stash drop
git stash clear
git stash branch
git stash show

git stash

暂存当前未提交的修改 自动采用历史提交作为默认注释

1
2
$ git stash
Saved working directory and index state WIP on main: f73bb62 Merge pull request #181 from Knogobert/main

git stash save

自定义注释

1
2
$ git stash save "message"
Saved working directory and index state On main: message

git stash -u

常规git stash只会暂存已跟踪的文件,但是如果想暂存新增又未跟踪的文件,那么可以使用-u参数,或者–include-untracked

1
2
$ git stash save 'untracked' -u
Saved working directory and index state On main: untracked

git stash -a

-a参数会暂存当前目录下所有修改,包括gitignore所忽略的文件,或者–all

1
2
3
4
5
$ git stash save 'a存储' -a
warning: in the working copy of '.husky/_/applypatch-msg', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of '.husky/_/commit-msg', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of '.husky/_/h', LF will be replaced by CRLF the next time Git touches it
...

git stash list

查看已暂存记录

1
2
3
$ git stash list
stash@{0}: WIP on main: f73bb62 Merge pull request #181 from Knogobert/main
stash@{1}: On main: untracked

git stash apply

默认恢复最新暂存记录,但是不会删除暂存记录,添加stash@{1}表示恢复指定暂存记录

1
2
3
4
5
6
7
8
$ git stash apply
On branch main
Your branch is behind 'origin/main' by 14 commits, and can be fast-forwarded.
(use "git pull" to update your local branch)

Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: components/content/inspira/ui/particle-image/new.vue

git stash pop

恢复最新暂存记录,同时会将缓存堆栈中的第一个stash删除,建议先apply再drop

1
2
3
4
5
6
7
8
$ git stash apply
On branch main
Your branch is behind 'origin/main' by 14 commits, and can be fast-forwarded.
(use "git pull" to update your local branch)

Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: components/content/inspira/ui/particle-image/new.vue

git stash drop

移除最新暂存记录,添加stash@{1}表示移除指定暂存记录

1
2
$ git stash drop
Dropped refs/stash@{0} (aa1e5cac7f59e2007bc1cdd04ba9324c23e6629a)

git stash clear

移除所有暂存记录

1
$ git stash clear

git stash branch

从最新暂存记录创建分支,如果成功,将会同时移除暂存记录,添加stash@{1}表示指定暂存记录

1
2
3
4
5
6
7
8
9
10
$ git stash branch feature_C stash@{1}
Switched to a new branch 'feature_C'
On branch feature_C
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: components/content/inspira/ui/particle-image/ParticleImage.vue

no changes added to commit (use "git add" and/or "git commit -a")
Dropped stash@{1} (2603b7bb4d4c604f680c434325e24d5fe5677742)

git stash show

查看暂存记录,添加-p或–patch参数显示具体修改内容,添加stash@{1}表示指定暂存记录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ git stash show stash@{1} -p
diff --git a/components/content/inspira/ui/particle-image/ParticleImage.vue b/components/content/inspira/ui/particle-image/ParticleImage.vue
index ca7d53b..cea7bc6 100644
--- a/components/content/inspira/ui/particle-image/ParticleImage.vue
+++ b/components/content/inspira/ui/particle-image/ParticleImage.vue
@@ -57,5 +57,8 @@ const imageParticleRef = ref<HTMLImageElement>();
onMounted(() => {
const { InspiraImageParticle } = inspiraImageParticles();
particles = new InspiraImageParticle(imageParticleRef.value);
+
+ console.log('onMounted B');
+
});
</script>

参考链接

[博客园_GitStash用法] https://www.cnblogs.com/codehome/p/14744919.html