git stash 操作

Posted GoldenaArcher

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了git stash 操作相关的知识,希望对你有一定的参考价值。

git stash 操作

git stash 是一个还挺方便,但是不会也不太要紧的指令,主要的应用场景为当部分的代码被修改了,但是又不想要 commit 当前的变化,便可以使用 git stash 将所有的变化进行保存,保持 working tree 干净。使用 git stash 会还原到上一个 commit 的状态。想要获取 stashed 的文件,就可以使用 git stash pop

换言之,使用 git commit + git revert --soft HEAD~1 的组合技也一样,就是多打了几个字会稍微麻烦一些。

stash list

可以用来显示所有的 stash 记录

stash

没什么卵用的补充 git stash 的完整指令是 git shtash store

大多数情况下来说,使用 git stash + git stash pop 就够了,不过 git stash 是一个栈(通过 pop 推测的),自然也可以存储多个 stash,查看 stashed 的记录可以使用 git stash list,如:

➜  stashing git:(main) git stash
Saved working directory and index state WIP on main: 2ea1a40 initial commit
➜  stashing git:(main) git stash
Saved working directory and index state WIP on main: 2ea1a40 initial commit
➜  stashing git:(main) git stash
Saved working directory and index state WIP on main: 2ea1a40 initial commit
➜  stashing git:(main)git stash -m "another lorem"
Saved working directory and index state On main: another lorem
➜  stashing git:(main) git stash list
➜  stashing git:(main) git stash list






















stash@0: On main: another lorem
stash@1: WIP on main: 2ea1a40 initial commit
stash@2: WIP on main: 2ea1a40 initial commit
stash@3: WIP on main: 2ea1a40 initial commit
(END)

友情提示,可以使用 -m 这个 flag 添加一些信息,这样之后回来看 stashed 的变化也知道做了些什么。

stash pop

直接 stash pop 会进行退栈操作,如:

➜  stashing git:(main) git stash list
































stash@0: On main: pop first
stash@1: On main: stash example
stash@2: On main: another lorem
stash@3: WIP on main: 2ea1a40 initial commit
stash@4: WIP on main: 2ea1a40 initial commit
stash@5: WIP on main: 2ea1a40 initial commit
(END)
➜  stashing git:(main) git stash pop
On branch main
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:   index.html

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@0 (780899dc334d6cd257fef560389fce018d314c08)
➜  stashing git:(main)git stash list






















stash@0: On main: stash example
stash@1: On main: another lorem
stash@2: WIP on main: 2ea1a40 initial commit
stash@3: WIP on main: 2ea1a40 initial commit
stash@4: WIP on main: 2ea1a40 initial commit
(END)

stash apply

使用 git stash pop <stash@index> 也可以选定指定的下标进行退栈操作,顺便,可以直接使用 index 而不需要输入完整的 stash@index,如:

➜  stashing git:(main) git stash list
































stash@0: On main: revert current change to avoid merge conflict
stash@1: On main: stash example
stash@2: On main: another lorem
stash@3: WIP on main: 2ea1a40 initial commit
stash@4: WIP on main: 2ea1a40 initial commit
stash@5: WIP on main: 2ea1a40 initial commit
(END)
➜  stashing git:(main) git stash pop 1
On branch main
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:   index.html

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@1 (19fbf2dd8719108de5d3d904a5752fe47e0f362c)
➜  stashing git:(main)git stash list






















stash@0: On main: revert current change to avoid merge conflict
stash@1: On main: another lorem
stash@2: WIP on main: 2ea1a40 initial commit
stash@3: WIP on main: 2ea1a40 initial commit
stash@4: WIP on main: 2ea1a40 initial commit
(END)

可以看到,栈顶 stashed 的文件没有被弹出,反而是 index 为 1 的对应 stashed 文件被弹出。

stash apply

git stash apply <stash@index> 指令与将对应 stashed 文件应用于当前分支上,但是并不会进行退栈操作,如果某一条 stashed 的记录需要确认在多条分支上都需要工作时,就可以使用这个指令,如:

➜  stashing git:(main) git stash list
































stash@0: On main: stash changes
stash@1: On main: revert current change to avoid merge conflict
stash@2: On main: another lorem
stash@3: WIP on main: 2ea1a40 initial commit
stash@4: WIP on main: 2ea1a40 initial commit
stash@5: WIP on main: 2ea1a40 initial commit
(END)
➜  stashing git:(main) git stash apply stash@2
On branch main
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:   index.html

no changes added to commit (use "git add" and/or "git commit -a")
➜  stashing git:(main)git stash list























stash@0: On main: stash changes
stash@1: On main: revert current change to avoid merge conflict
stash@2: On main: another lorem
stash@3: WIP on main: 2ea1a40 initial commit
stash@4: WIP on main: 2ea1a40 initial commit
stash@5: WIP on main: 2ea1a40 initial commit
(END)

stash drop

git stash drop <stash@index> 用于删除对应 stashed 的变化,与 apply 对应,如:

➜  stashing git:(main)git stash list






















stash@0: On main: stash changes
stash@1: On main: revert current change to avoid merge conflict
stash@2: On main: another lorem
stash@3: WIP on main: 2ea1a40 initial commit
stash@4: WIP on main: 2ea1a40 initial commit
stash@5: WIP on main: 2ea1a40 initial commit
(END)
➜  stashing git:(main)git stash drop 1
Dropped refs/stash@1 (1d6a06ed2ac0b17c6380a509224932c9137afd7e)
➜  stashing git:(main)git stash list



















stash@0: On main: stash changes
stash@1: On main: another lorem
stash@2: WIP on main: 2ea1a40 initial commit
stash@3: WIP on main: 2ea1a40 initial commit
stash@4: WIP on main: 2ea1a40 initial commit
(END)

其他

使用 stash 相关指令,尤其是应用变化(pop,apply)时可能会产生 merge conflict,这时候就比需要使用 commit 去解决 merge conflict。这也就是为什么说会用 stash 还挺方便的,不会的话新建 branch+commit+reset 也一样使用。毕竟需要修 merge conflict 的时候,总归都是要产生一个 commit 的。

以上是关于git stash 操作的主要内容,如果未能解决你的问题,请参考以下文章

如何查看git stash内容

git stash 操作

git stash 操作

git stash操作

git stash 的一次惊心动魄的误删操作

Git命令之stash