LoginSignup
7
6

More than 5 years have passed since last update.

gitでaddする前の状態で別ブランチに移動したくなった時に変更内容を避けておく方法。

Last updated at Posted at 2015-09-14

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で避ける前の状態に戻すことができる。

参照

Git-のさまざまなツール-作業を隠す

7
6
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
7
6