LoginSignup
0
0

More than 1 year has passed since last update.

error: the following files have local modifications:

Last updated at Posted at 2021-06-30

事象 : とあるディレクトリ配下をGit管理対象外にしようとしたら怒られた

# .gitignoreを書いて
$ cat ../.gitignore
mkdocs/site/

# さぁ、削除しよ・・・としたら怒られた
$ git rm -r site/
error: the following files have local modifications:
    mkdocs/site/404.html
(use --cached to keep the file, or -f to force removal)

原因 : ローカルに変更があるから

メッセージを和訳すると「エラー:次のファイルにローカル変更があります。」でした。

$ git status
On branch feature/mkdocs
Your branch is up to date with 'origin/feature/mkdocs'.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   site/404.html
no changes added to commit (use "git add" and/or "git commit -a")

対応1 : -fオプションをつけてgit rmする

ローカルの変更なんていらない!Git管理対象外にするファイルも削除しちゃってOK!な時の対応。

git rm will not remove a file from just your working directory.
(和訳)git rmは、作業ディレクトリからファイルを削除しません。
Git - git-rm Documentation

基本は「作業ディレクトリからファイルを削除しません。」がこのやり方ではファイルが作業ディレクトリから削除されちゃうので注意して、自己責任で実行してください。

The files being removed have to be identical to the tip of the branch, and no updates to their contents can be staged in the index, though that default behavior can be overridden with the -f option.
(頑張った和訳)削除されるファイルは、ブランチの先端と同じである必要があり、その内容の更新をインデックスにステージングすることはできません。ただし、デフォルトの動作は-fオプションで上書きできます。
Git - git-rm Documentation

# -fオプションでローカルの変更チェックを無視!
$ git rm -fr site/
rm 'mkdocs/site/404.html'

# めでたく削除されたので後はコミット&プッシュする
$ git status
On branch feature/mkdocs
Your branch is up to date with 'origin/feature/mkdocs'.
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        deleted:    site/404.html

他の対応方法

今回はやってないけど将来のためにメモ

  • 作業ディレクトリにファイルは残おかない場合 : ローカルの変更を消すなりコミットするなりする
  • 作業ディレクトリにファイルは残しておきたい場合 : git rm --cached {ファイル}する
git rmのオプション 意味 参考サイト
-r ディレクトリが指定された場合、再帰的に削除する Git - git-rm Documentation
-f
--force
最新のチェックを上書きする 【Git】特定の名前のフォルダ配下のファイルを管理対象外にする|coglay Blog
--cached インデックスからだけ削除して、作業ディレクトリにファイルは残す [git]commitした後に.gitignoreに追加したい時 | あくあ|development_blog
0
0
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
0