LoginSignup
47
40

More than 5 years have passed since last update.

[Git]変更を一時的に退避するgit stashの使い方イロハ

Last updated at Posted at 2015-07-30

git stash

git stashコマンドは、一時的に変更を退避するために使われます。

実際には以下のように使われます。

$ git status # 差分を見る
On branch master
Your branch is up-to-date with 'origin/master'.
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:   README.md

no changes added to commit (use "git add" and/or "git commit -a")

$ git stash # 変更を退避
Saved working directory and index state WIP on master: 21ac30e Update sample scripts
HEAD is now at 21ac30e xxxxxxxxxxxxxxxxxxxxx

$ git status # 確認
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean

$ git stash list # 退避されている変更を確認
stash@{0}: WIP on master: 21ac30e xxxxxxxxxxxxxxxxxxxxx

$ git stash pop # 取り出し
On branch master
Your branch is up-to-date with 'origin/master'.
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:   README.md

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (0ab136bef6df794fa9aff6e9c2e3fadfddcef13a)

サブコマンド

git stash list

git stash listを用いると、保存されている変更分の一覧が確認できます。

$ git stash list
stash@{0}: WIP on master: 21ac30e xxxxxxxxxxxxxxxxxxxxx

git stash apply

git stash popでは、保存した変更分を取り出して再び反映させた後、
保存されていた変更分は消えてしまいます。

git stash applyで取り出すと、変更分は削除されません。

$ git stash list
stash@{0}: WIP on master: 21ac30e xxxxxxxxxxxxxxxxxxxxx

$ git stash apply
On branch master
Your branch is up-to-date with 'origin/master'.
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:   README.md

no changes added to commit (use "git add" and/or "git commit -a")

$ git stash list
stash@{0}: WIP on master: 21ac30e xxxxxxxxxxxxxxxxxxxxx

git stash {pop|apply} stash@{x}

$ git stash apply stash@{1}

git stash {pop|apply}は、番号を指定すると、上述のgit stash listで確認できるstash番号を参照して、保存されている作業を取り出すことができます。

git stash save '任意の文字列'

例えば、普通にgit stashを実行するのではなく、上のようにメッセージ付きで退避することが可能です。
このようにして保存した作業は、git stash listで以下のように確認できます。

$ git stash save '不要な関数の削除'
$ git stash list
stash@{0}: On dev: 不要な関数の削除

git stash show stash@{x}

退避した変更分を取り出さずにstashの差分を確認することができます。

$ git stash show stash@{0}
 README.md | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

ユースケース

リモートの変更分をPullするときにローカルの変更を一時退避する

$ git pull                                                                                                                 Updating 21ac30e..397a42e
error: Your local changes to the following files would be overwritten by merge:
        README.md
Please, commit your changes or stash them before you can merge.
Aborting

$ git stash

$ git pull
Updating 21ac30e..397a42e
Fast-forward
 README.md | 8 ++++++++
 1 file changed, 8 insertions(+)

$ git stash pop

まとめ

git stashはとても便利なコマンドなので、
適切に使う事ができれば開発の大きな助けになります。
サブコマンドを駆使することでgit stashをもっと快適に使いつつ、
快適なGitライフを送りましょう!

47
40
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
47
40