LoginSignup
204
177

More than 5 years have passed since last update.

ブランチを作り忘れた時

Last updated at Posted at 2013-07-07

※2013.07.08 追記あり

ブランチを作成するのを忘れて作業してて、masterを編集してしまった。
途中からブランチを作成して、そこで作業を再開する方法。

現在のワークツリーを一時的に保存する

git stash

stashに保存されている状態のリストを確認

git stash list
stash@{0}: WIP on master: 0f043fe Finish layout and routes

ブランチ(working)を作成する

git branch working
git checkout working

保存した状態(stash@{0})を現在のブランチに適用する

git stash pop

実行後に適用した状態は削除される
削除したくない場合は、

git stash apply

workingブランチを作成出来たので、
今後はこのworkingブランチを使用して作業をする。

--
2013.07.08追記
acple@githubさんから指摘をいただきました。ありがとうございます。
まだコミットしていない場合は、git checkout -b working(ブランチを切り替える)だけでOKとのことです。
試してみます。

masterブランチのsample.txtを変更する

git status
# On branch 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:   sample.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

ブランチ作成、切り替え

git checkout -b working
git status
# On branch working
# 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:   sample.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

ちゃんとブランチが変更され、変更したファイルも見えていますね。
これをコミットすれば、workingブランチにコミットされます。

コミットされるまでブランチは分かれないので、変更しているファイルは同じなんですね。
workingブランチを作成後にブランチをmasterに切り替えて、sample.txtを開くと変更されていませんでした。
ブランチについて少し理解が深めることが出来ました。
ありがとうございました。

git stashの使いどころについても今後少し調べてみよう。

204
177
4

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
204
177