LoginSignup
261
180

More than 5 years have passed since last update.

untracked fileを削除するためにはgit cleanを使う

Posted at

はじめに

mergeするためにbranchからmasterに戻った際、誤ってbranchで新規作成したファイルを保存してしまいmergeできなくなってしまいました。
いろいろググって解決はしましたが、この状況に対してピンポイントな対応方法についての記事がなかったので解決手順をメモします。
(こんなミスはなかなかしないでしょうからピンポイントな記事がないのもしょうがないですね。。。といいますかエラーを見れば原因がすぐわかりますからね)

mergeできなくなった際の状況

masterに戻ってgit merge ブランチ名を実行したら以下のエラーが出ました。

$ git merge ブランチ名
Updating 67b73c9..98ba96a
error: The following untracked working tree files would be overwritten by merge:
                (省略)
        spec/features/categories_spec.rb
Please move or remove them before you merge.
Aborting

git statusでは以下のようになっていました。

On branch master
Your branch is ahead of 'origin/master' by 32 commits.
  (use "git push" to publish your local commits)

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

        (省略)
        spec/features/categories_spec.rb

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

つまりはuntracked fileを削除しないとmergeできない状況になっているということですね。

対応方法

untracker file

まずuntracked fileについて理解します。
詳細な内容は上記リンク先に譲りますが、簡単にまとめると
untracked fileとは、前回のcommitの時点では存在しなかった新たに作成したfileであり、かつgit addしてステージさせていないfileのことです。

git clean

untracked fileを削除するためにはgit cleanを使います。
使用したコマンドは以下です。

git clean -n
untracked fileを削除する前に、削除の対象となるファイルを確認できます。
ディレクトリを確認したい場合はgit clean -dnで確認できます。

git clean -f
untracked fileを削除します。
先ほどと同様にディレクトリを削除する場合はdをつけます。 git clean -df

その他参考サイト

https://ja.atlassian.com/git/tutorials/undoing-changes/git-clean

261
180
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
261
180