在當前分支開發過程中,突然有緊急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}