2014/07/31

使用 git stash 保存工作狀態 ( Save working status by `git stash` )


[ Quick Solution Mode ]
$ git stash                # save working status
$ git stash save "$name"   # save working status with a specific name ($name)
$ git stash list           # List the stashes that you have
$ git stash pop            # popup and remove the latest stash
$ git stash apply          # get the latest stash but keep the stash
$ git stash drop stash@{x} # drop a record named stash@{x}
$ git stash show stash@{x} # show changed files named stash@{x}
$ git stash show -p stash@{x}    # show stash@{x} detail diff 
$ git stash --include-untracked  # to stash untracked file

[ Verbose Mode ]
使用 git 做版本控管時,
有一個很好用的功能,
就是用 git stash 來暫存你的變更,
這麼做有什麼好處跟目的?

思考一下開發時可能遇到的情形...
開發到一半,臨時需要切換到另一個 branch 開發,
有可能是突然想到另一個 branch bug的解法,
或被 assign 先處理另一件事,
或是想暫時恢復未 commmit 前的狀態, 看看修改前的狀況,
不管如何,就是有需要切換 branch 或恢復成未修改的狀態,
但目前的功能做一半,又不好 commit 一個半成品,

這個情況就可以用 stash 來將目前所有的修改儲存起來,(有點像暫存區)
等到需要時再取出來, 繼續開發,
`git satsh` 的 help 的說明如下...
`git stash` help:
usage: git stash list []
   or: git stash show []
   or: git stash drop [-q|--quiet] []
   or: git stash ( pop | apply ) [--index] [-q|--quiet] []
   or: git stash branch []
   or: git stash [save [--patch] [-k|--[no-]keep-index] [-q|--quiet]
         [-u|--include-untracked] [-a|--all] []]
   or: git stash clear
可以查 Git Manual, 取得更詳細的說明
$ git stash --help or man git stash
GIT-STASH(1)                      Git Manual                      GIT-STASH(1)

NAME
       git-stash - Stash the changes in a dirty working directory away

SYNOPSIS
       git stash list []
       git stash show []
       git stash drop [-q|--quiet] []
       git stash ( pop | apply ) [--index] [-q|--quiet] []
       git stash branch  []
       git stash [save [--patch] [-k|--[no-]keep-index] [-q|--quiet]
                    [-u|--include-untracked] [-a|--all] []]
       git stash clear
       git stash create

簡單來說, 要保存到暫存區,
可以執行
$ git stash                 # 自動保存
# OR
$ git stash save "$name"    # 存成自訂的名字, 好處是可讀性高, 如果保存了好幾筆記錄,
                              才不會看不出哪個是哪個

$ git stash list            # 保存後, 可用此來查看暫存區內保存了哪些暫存記錄
$ git stash show            # 用來查看某筆暫存記錄變更的檔案和內容

$ git pop                   # 直接取出最後一筆暫存記錄
$ git pop "$name"           # 取出某一筆暫存記錄

$ git drop "$name"          # 取出記錄繼續開發後,就不再需要這筆暫存記錄了,用 drop 來刪除吧
                              eg. git stash drop stash@{1}

沒有留言:

張貼留言