問題
- 編碼時,有時會出現代碼寫了一半,還沒寫完,但是需要中斷去執行別的操作,例如:切換到另外一個分支去修改時,此時不能提交代碼,也不能丟棄代碼更改,所以需要能夠臨時保存下代碼修改。
git stash
使用
xxx@midcompser:~$ git stash -help
usage: git stash list [<options>]
or: git stash show [<options>] [<stash>]
or: git stash drop [-q|--quiet] [<stash>]
or: git stash ( pop | apply ) [--index] [-q|--quiet] [<stash>]
or: git stash branch <branchname> [<stash>]
or: git stash clear
or: git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]
[-u|--include-untracked] [-a|--all] [-m|--message <message>]
[--] [<pathspec>...]]
or: git stash save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]
[-u|--include-untracked] [-a|--all] [<message>]
git stash
git stash save 'message...' //保存並增加註釋信息
方法1:
git stash -p //交互模式,選擇需要保存的文件
方法2:
git stash push <file1> <file2> <file3> [file4 ...] //新版本才支持
* git stash push include/.
git stash list
git stash pop
git stash pop --index
* 恢復指定的進度到工作區。index 從 0 開始
git stash pop stash@{1}
* 恢復指定的進度到工作區。stash@{1} 是通過git stash list命令得到的
* 通過git stash pop命令恢復進度後,會刪除保存的stash記錄。
git stash apply [–index] [stash_id]
* 除了不刪除恢復的進度之外,其餘和git stash pop 命令一樣。
git stash drop [stash_id]
刪除一個存儲的進度。如果不指定stash_id,則默認刪除最新的存儲進度。
git stash clear
刪除所有存儲的進度。
注意點
- stash會保存暫存區和工作區已加入版本管理的文件修改,但是pop後,之前已加入暫存區的文件不會恢復到暫存區,而是恢復到工作區。