LoginSignup
1
0

More than 3 years have passed since last update.

【Git】中途半端でコミットしたく無いけど別の作業がしたい!(git stash)

Last updated at Posted at 2020-02-21

Git コマンド紹介:stash
まあまあ使うので、まとめておきます。

stash

こんな時に使う

作業が中途半端だ!コミットしたく無い!でも、別のブランチにチェックアウトしたい!

使用例

git stash save:いったん保存

git checkout 別branch
別の作業...
git checkout 元branch

git stash list:保存したものの確認

git stash pop stash@{スタッシュ番号}:保存してたものを戻す(listから消す)
or
git stash apply stash@{スタッシュ番号}:保存してたものを戻す(listに残す)

解説

% git sss
## master...origin/master [ahead 1]
 M index.html

今、masterブランチで作業していて、index.htmlが未コミットです。
stashコマンドを使っていきます。

% git stash save
Saved working directory and index state WIP on master: 6c22e4e add directory

保存できました。これで、別ブランチにチェックアウトしてOK。
branchブランチで別の作業をします。

% git checkout branch
Switched to branch 'branch'

作業...

% git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

作業が終わりmasterブランチに戻ってきました。

% git stash list
stash@{0}: WIP on master: 6c22e4e add directory

保存がしっかり確認できます。これから、戻していきます。

apply(保存を残す)

% git stash apply stash@{0}
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

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:   index.html

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

これで無事masterでの作業に戻れました。ちなみに、

% git stash list
stash@{0}: WIP on master: 6c22e4e add directory

stash applyはlistに残ります。

pop(保存を残さない)

% git stash pop stash@{0}
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

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:   index.html

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

% git stash list

stash popはlistに残りません。


その他のstashコマンド

  • git stash save "コメント":コメントをつけて保存
  • git stash drop stash@{スタッシュ番号}:listから消す
  • git stash clear:listから全て消す
1
0
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
1
0