今天正在Coding,組長來了個臨時BUG修復(fù)。功能已經(jīng)Coding到一半,這時候想起之前看過的Git使用stash保存工作。然后就去了解了一下相關(guān),并做一下記錄。
1、What
stash指令有什么用?
stash 是 git 的常用命令之一,用于管理臨時工作,包括新增保存,查看,移除,等等。
2、How
這里總結(jié)一下常用姿勢。
- 所有命令
git stash list [<options>]
git stash show [<stash>]
git stash drop [-q|--quiet] [<stash>]
git stash ( pop | apply ) [--index] [-q|--quiet] [<stash>]
git stash branch <branchname> [<stash>]
git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]
[-u|--include-untracked] [-a|--all] [-m|--message <message>]]
[--] [<pathspec>…?]]
git stash clear
git stash create [<message>]
git stash store [-m|--message <message>] [-q|--quiet] <commit>
- 存儲
git stash
git stash push -m "Im stash log" //添加備注的方式
還有文章介紹使用 git stash save -m "log" ,但是官方文檔說deprecated了,推薦使用 push。

- 查看
git stash list //顯示所有的stash
git stash show stash@{id} //查看某個stash的詳細情況,所有文件的狀態(tài)


- 恢復(fù)
git stash pop //恢復(fù)最近的一次
git stash pop stash@{id} //恢復(fù)某一次stash
- 移除
git stash drop stash@{id} //移除某一stash
git stash clear //清除所有stash
3、Note
- 不會保存未track的文件,比如新建了文件,但未add
- 每次最新stash的記錄,下標(biāo)都為0,之前的stash下標(biāo)+1,所以恢復(fù)指定stash的時候注意了。
- 恢復(fù)可能會產(chǎn)生沖突。官方原文說明:
Applying the state can fail with conflicts; in this case, it is not removed from the stash list. You need to resolve the conflicts by hand and call git stash drop manually afterwards.
恢復(fù)是使用auto-meger進行合并的,當(dāng)meger產(chǎn)生沖突時,pop操作會恢復(fù)工作內(nèi)容,并標(biāo)記沖突,但是不移除stash list的記錄。當(dāng)你解決沖突后,需要手動drop移除掉stash list的記錄。當(dāng)你解決沖突后,需要手動drop移除掉stash。
這樣能夠完全保證用戶的工作內(nèi)容不會受到?jīng)_突影響。若你擔(dān)心有什么問題,或者不想這樣解決沖突,你可以完全拋棄本次pop,直接reset回到之前stash時的節(jié)點,再pop出來,這樣就不會有沖突問題。在本次工作完成后,push提交時候再進行沖突處理。


