Help us understand the problem. What is going on with this article?

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

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から全て消す
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした