1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Git 間違ってmasterで作業してしまった時のコマンド

1
Last updated at Posted at 2020-02-14

思わずdevelopmenntのつもりで作業してしまった! または止むを得ずmasterで作業せざるを得なかった場合のコマンド備忘録

特にデプロイ確認の際には本番環境で作業せざるをえない。

作業内容を退避させる

$ git stash save

これでmasterで作業した分を一時保存

新ブランチに移動

$ git checkout -b development

新ブランチで作業内容を復元

$ git stash pop

これにて マスターの作業内容が無事に新ブランチに移ったとさ。 チャンチャン!


2026年3月 追記(最新情報)

この記事は2020年2月に投稿されました。

デフォルトブランチ名の変更

GitHub(2020年10月)・GitLab(2021年5月)のデフォルトブランチが master から main に変更されています。新規リポジトリでは main が標準です。この記事の master は、お使いのリポジトリに合わせて main に読み替えてください。

git stash コマンドの変更

  • git stash save は非推奨です(Git 2.13+)。代わりに以下を使用してください:
# メッセージ付きstash(推奨)
git stash push -m "作業中の変更を退避"

# 特定ファイルだけ退避
git stash push file1.rb file2.rb

# ステージされた変更のみ退避(Git 2.35+)
git stash push --staged

より良いワークフロー

stashは一時的な退避手段です。長時間の作業中断が見込まれる場合は、stashではなくブランチを作成することを推奨します:

git switch -c temp/wip-feature
git commit -m "WIP: 作業途中"
1
3
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
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?