首頁·終端機命令

git stash

git

用法

git stash [push] [-u] [-m "<message>"]

把沒做完的改動先收起來,讓工作區變乾淨;不加 -u 時未追蹤檔案會留在原地,pop 取出後會刪掉那筆記錄,apply 則保留。

常用選項

選項含義
-uinclude untracked files, which are otherwise left behind
-ainclude ignored files too
-kkeep the staging area as it is
-pstash selected hunks
popapply the newest entry and delete it
applyapply it and keep the entry
listshow the entries
drop <stash>delete one entry

範例

git stash -u -m "wip login"

parks tracked and untracked changes under a name

git stash pop

brings the newest stash back and removes it from the list

git stash apply stash@{2}

applies an older entry and leaves it in the list

難的地方在於復原有好幾種:把分支的指向挪走,和補一個反向提交,不是一回事。

怎麼看

  • 方括號 [ ] 表示這部分可以不寫。
  • 省略號 … 表示可以寫多個。
  • 選項區分大小寫——有些命令裡 -r 和 -R 不是一回事。

常見問題

Q. git stash 是做什麼的?

把沒做完的改動先收起來,讓工作區變乾淨;不加 -u 時未追蹤檔案會留在原地,pop 取出後會刪掉那筆記錄,apply 則保留。

Q. 怎麼寫?

git stash [push] [-u] [-m "<message>"] —— 方括號表示可以省略的部分。

Q. 值得記的選項有幾個?

這裡列了 8 個,完整列表在 man git。這條命令屬於git。

相關命令

man 手冊: man git-stash