#gitでaddする前の状態で別ブランチに移動したくなった時に変更内容を避けておく方法。
例えばこういう場合。
$ git status
On branch hogehoge_20150914
Untracked files:
(use "git add <file>..." to include in what will be committed)
hoge.php
nothing added to commit but untracked files present (use "git add" to track)
この状態でブランチを移動しようと知ると、
$ git checkout master
error: Your local changes to the following files would be overwritten by checkout:
hoge.php
Please, commit your changes or stash them before you can switch branches.
Aborting
と怒られる。
##そういうときはgit stash
を使う
$ git stash
Saved working directory and index state WIP on hogehoge_20150914: 3a158e0 '直前のコミットの内容'
##もう一度git status
して状態を見てみる
$ git status
# On branch hogehoge_20150914
nothing to commit, working directory clean
##これで安心してbranchを移動できる
$ git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
##git stash
で避けていた変更内容を元に戻すとき"
$ git stash list
stash@{0}: WIP on dev_auto_point_log_20150914: 3a158e0 '直前のコミットの内容'
でstashしたlistを見ることができる。
さらに、git stash apply
をすると、
$ git stash apply
On branch dev_auto_point_log_20150914
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: hoge.php
no changes added to commit (use "git add" and/or "git commit -a")
stashで避ける前の状態に戻すことができる。
#参照