- git stashとは
- いつ使えるか
- 使い方
- おわりに
1. git stashとは
DESCRIPTION
Use git stash when you want to record the current state of the working directory and the index, but want to go back to a clean working directory. The command saves your local modifications away and reverts the working directory to match the HEAD commit.
参考 Git
簡単にまとめると
現状作業ディレクトリの変更箇所 → 退避(格納) → 好きなタイミングで再度適用可能
git stash
によって、現在の作業ディレクトリでの変更箇所(追跡しているファイルのうち変更されたもの、そしてステージされた変更)を退避させることができ(実行後は変更前の状態に戻る)、用途に合わせて好きなタイミングや別ブランチに移動したあとで、その変更記録をゴソッと再度適用することができます。
2. いつ使えるか
・作業するブランチを間違ってしまったとき
私はこのパターンで、本来ならば別ブランチでするべき実装を、意図していない作業ブランチでしてしまっておりました。
・やむを得ず現在の作業ブランチから離れて、別ブランチで作業しなければならないとき
作業の優先順位が変わってしまった時に使えそうです。
3.使い方
基本
git stash
で実行。
git stash list
でstashしたものの確認。stash@{0}
←この数字が若いほど新しい。
stash@{0}: WIP on submit: 6ebd0e2... Update git-stash documentation
stash@{1}: On master: 9cc0589... Add git-stash
stash@{stashナンバー} <ブランチ名(stash時の)> <ハッシュ(stash時のHEADの)> <コミットメッセージ(stash時のHEADの)>
git stash pop
最新のstashを適用。同時にそのstashの削除
上記で言う stash@{0}: WIP on submit: 6ebd0e2... Update git-stash documentation
が適用されて、listからなくなる。
git stash apply
最新のstashを適用。そのstashは残る。
上記で言う stash@{0}: WIP on submit: 6ebd0e2... Update git-stash documentation
が適用されるが、listに残ったまま。
git stash apply stash@{2}
のようにstash番号を指定して適用させることも可能。
git stash show
変更差異の確認。オプションなしで最新stash。stash@{確認したい番号}
で指定可能。
app/views/posts/_post_contents.html.erb | 1 +
1 file changed, 1 insertion(+)
git stash drop
stashの削除。オプションなしで最新stash。stash@{削除したい番号}
で指定可能。
Dropped stash@{0} (575530adb53095ceb43a514fde80ab5c8003b65a)
git stash clear
stashの全削除。不要なstashは消していきましょう。
その他オプション
いろいろとオプションはあります。
usage:
git stash drop [-q|--quiet] [<stash>]
git stash ( pop | apply ) [--index] [-q|--quiet] [<stash>]
git stash branch <branchname> [<stash>]
git stash save
[--patch] [-k|--[no-]keep-index] [-q|--quiet]
[-u|--include-untracked] [-a|--all] [<message>]
git stash
[push [--patch] [-k|--[no-]keep-index]
[-q|--quiet][-u|--include-untracked] [-a|--all] [-m <message>][-- <pathspec>...]]
興味のある方は、こちらから詳細確認してください
4.おわりに
ご指摘等ございましたら、ご一報いただけると幸いです。