git stash暂存本地代码

在当前分支开发过程中,突然有紧急BUG需要切换分支修改,但你本地已经存在代码,需要push之后,才能切换分支
这个时候就可以使用git stash,进行暂存

指令如下:

# 保存当前未commit的代码
git stash

# 保存当前未commit的代码并添加备注
git stash save "备注的内容"
git stash -m '备注的内容'
# 列出stash的所有记录 git stash list # 删除stash的所有记录 git stash clear # 应用最近一次的stash git stash apply
git stash apply stash@{2} # 应用最近一次的stash,随后删除该记录 git stash pop
git stash pop stash@{2} # 删除最近的一次stash git stash drop
git stash drop stash@{2}

# 清除stash记录后如何恢复
(1、找到被clear的stash
git log --graph --oneline --decorate $( git fsck --no-reflog | awk '/dangling commit/ {print $3}' )
(1.1 也可以把查记录信息创建并写入1.txt文件里边,打开文件ctrl+F搜索stash的保存信息
git log --graph --oneline --decorate $( git fsck --no-reflog | awk '/dangling commit/ {print $3}' ) >1.txt
(2、恢复stash
git stash apply ff859c708(查询的记录id)

当有多条 stash,可以指定操作stash,首先使用stash list 列出所有记录:

$ git stash list
stash@{0}: WIP on ...
stash@{1}: WIP on ...
stash@{2}: On ...

应用第二条记录:(pop,drop 同理。)

git stash apply stash@{1}

 

posted @ 2022-04-21 11:24  40度丶仰望  阅读(804)  评论(0编辑  收藏  举报