[TOC] #### 1. 應用場景 --- git stash 命令用于將工作區中的更改和暫存區中的內容儲存起來 日常開發中,會經常遇到這種場景 我們正在 dev 分支開發新功能,做到一半時,產品經理過來和我們反饋了一個緊急 bug,需要馬上解決,但是做了一半的新功能又不想提交到版本庫。這時可以使用 `git stash push` 先將當前進度保存起來,然后去修復 bug,修復完后使用 `git stash apply` 恢復之前保存的進度即可 場景1、需要切換到另一個分支(master)處理 bug ``` git stash push -m '功能開發中' git checkout master ``` 場景2、需要回到新功能編寫前的狀態,也就是 dev 分支的最新提交記錄 ``` git stash push -m '功能開發中' ``` 使用 git stash 的前提必須是版本庫中已有提交記錄,否則會出現下面提示 ``` $ git stash # 您還沒有初始提交 You do not have the initial commit yet ``` 沒有可以儲存的內容(工作區中沒有更改,暫存區中也沒有內容) ``` $ git stash # 沒有要保存的本地更改 No local changes to save ``` #### 2. 添加儲藏 --- 添加儲藏 ``` git stash ``` `-m,--message` 添加儲藏和備注信息 ``` git stash push -m <message> ``` #### 3. 查看儲藏 --- 查看所有儲藏(所有分支共享儲藏內容,而不是像提交記錄每個分支都是獨立的) ``` git stash list ``` 查看文件變動差異 ``` # 顯示哪些文件變動了幾行 git stash show <stash> # 顯示更加詳細的變動信息,可以看到新增、減少了什么內容 git stash show -p <stash> ``` #### 4. 刪除儲藏 --- 刪除某個儲藏 ``` git stash drop <stash> ``` 清除所有儲藏 ``` git stash clear ``` #### 5. 使用儲藏 --- 應用儲藏 ``` # 應用指定的儲藏 git stash apply <stash> # 應用并刪除指定的儲藏 git stash pop <stash> ``` `<stash>` 指的是 `git stash list` 命令輸出結果左側的值,如下圖所示  ``` # 錯誤:您對以下文件的本地更改將被“合并”覆蓋 error: Your local changes to the following files would be overwritten by merge: 1.txt # 請在合并前提交或隱藏更改 Please commit your changes or stash them before you merge. ``` #### 6. 常見用法 --- ``` # 添加存儲 git stash push -m <message> # 查看所有存儲 git stash list # 查看儲藏文件差異 git stash show -p <stash> # 應用儲藏 git stash apply <stash> # 應用并刪除儲藏 git stash pop <stash> # 刪除指定儲藏 git stash drop <stash> # 清空儲藏 git stash clear ```