0
2

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 stashを使った未コミット変更エラーの解決手順をまとめてみた

Posted at

はじめに

Gitを使ってブランチを切り替える際に発生しがちな「未コミット変更があってcheckoutできない」問題。

個人の備忘録程度の走り書きとなっておりますが、温かい目で見守っていただければ幸いです。

これを解決しつつ、正しい手順で「mainブランチへの切り替え」「最新の取り込み」まで完了した過程をまとめます。

書こうと思ったきっかけ

Gitでブランチを切り替えようとしたとき、「未コミット変更があってcheckoutできない」というエラーに遭遇。

その後、git stashで変更を一時的に保存し、checkoutし、最新のコミットをpullして正常状態へ戻しました。例えのスムーズな手順を残しておくために記事化することにしました。

実際に起きたエラーと使ったコマンドまとめ

起きたエラー

 ~/Desktop/.../frontend (git)-[feature/add-login-pin-dialog]- git checkout main
error: Your local changes to the following files would be overwritten by checkout:
	src/app/page.tsx
Please commit your changes or stash them before you switch branches.
Aborting

実行したコマンド

  1. 作業中の変更を一時退避する
git stash

出力:

Saved working directory and index state WIP on feature/add-login-pin-dialog: 92541d5 管理ユーザー選択時にPINコードダイアログを表示する機能を追加およびポエム認証削除
  1. mainブランチへ切り替える
git checkout main

出力:

Switched to branch 'main'
Your branch is behind 'origin/main' by 13 commits, and can be fast-forwarded.
  (use "git pull" to update your local branch)
  1. 最新の変更を取り込む
git pull origin main

出力(抜粋):

Updating dc87b1d..1856b92
Fast-forward
.github/workflows/cd.yml |  43 +++++++++++++
.github/workflows/ci.yml |  60 +++++++++++++++++++
...

まとめ

  • git stash を使い、未コミットの変更を一時送り、checkoutを完了
  • git checkout main でmainブランチへ切り替え
  • git pull origin main でリモートの最新状態を取り込み
  • この流れで「未コミットデータがあるままブランチ切り替え失敗」を解決できた

次は、新しいブランチを切るか、stashした作業をpopして続きの作業をするかという流れになります...!

0
2
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
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?