LoginSignup
14
18

More than 5 years have passed since last update.

git checkout で error: Your local changes to the following files would be overwritten by checkoutと言われる解決方法

Posted at

git checkoutコマンドでエラーがでました。ここではhoge/pugiemonnというい名前のブランチにcheckoutをしようとしています。

checkoutでエラー
git checkout hoge/pugiemonn
error: Your local changes to the following files would be overwritten by checkout:
    hoge.php
Please, commit your changes or stash them before you can switch branches.
Aborting

hoge.phpが変更されているために、checkoutで上書きされる可能性があるため、commitするかstashしろと書いてあります。

このような場合状況によって解決方法が変わると思いました。

1. commitして解決

hoge.phpが変更されているので、変更をcommitします。

変更をcommitする
git add hoge.php
git commit -m "hoge.phpを追加"

その後、git checkout hoge/pugiemonnするとcheckoutが通ります。

2. stashして解決

hoge.phpの変更をとっておきたいけれど、いまはcommitしたくない場合はstashします。

stashする
git stash

その後、git checkout hoge/pugiemonnするとcheckoutが通ります。

3. checkoutして解決

hoge.phpの変更を無効にする場合はcheckoutします。ただしcheckoutの場合はhoge.phpにあった変更は全て消えてしまいます。

checkoutする
git checkout hoge.php

その後、git checkout hoge/pugiemonnするとcheckoutが通ります。

14
18
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
14
18