LoginSignup
7
4

More than 5 years have passed since last update.

コミットしようと思ったら違うブランチにいた場合の対処

Posted at

状況

いい感じでソースをガリガリ書いていた。
テストもいい感じだ。
さあコミットしましょう!
念のため今のブランチを確認する。

$ git branch

featureブランチで作業してると思ったら develop ブランチじゃん!!

対処

もしaddしちゃってたら取り消す

$ git reset HEAD project/hoge.php

変更したファイルを退避

$ git stash

これで変更ファイルが退避されるので今の状況を確認してみる。

$ git status
On branch develop
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        project/fuga.php

nothing added to commit but untracked files present (use "git add" to track)

修正した project/hoge.php はリストから消えていて、新規追加した project/fuga.php はそのまま残っている。

git stash save "message"
にすると、何の作業中ソースなのかコメントを残せる

本来のブランチを作ってなかった場合はここで作る
今回は git flow で feature/piyo ブランチを作る

$ git flow feature start piyo

上記で新しく feature/piyo が作られ、カレントブランチも移動される。

ここで、さっき退避したファイルを戻す

$ git stash apply

git stash は、複数退避させて指定したものを戻す、ということもできる
apply は最新のを対象に戻す時に指定する

確認してみる

$ git status
On branch feature/piyo
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:   project/hoge.php

Untracked files:
  (use "git add <file>..." to include in what will be committed)


        project/fuga.php

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

あとはいつも通り

そして、もう要らなくなったものは綺麗にお掃除しておく

現在のリスト

$ git stash list
stash@{0}: WIP on develop: cd4e171 ・・・・・
stash@{1}: WIP on branch1: 2345789 ・・・・・

指定して削除

$ git stash drop <stashのNo.>

または、全部削除

$ git stash clear

参考にさせていただいたページ

3 Git のさまざまなツール - 作業を隠す
https://gist.github.com/koudaiii/526707492ebc5915596e
https://gist.github.com/dtan4/5276185

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