0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

GitHub とローカル Git の両方から追跡済みファイル・フォルダを削除する方法

0
Last updated at Posted at 2026-03-08

Git で一度 addcommit して
追跡対象になったファイル・フォルダを削除したい場合、
単にローカルで削除するだけでは Git の管理からは消えません。

この記事では ローカル Git と GitHub
の両方から削除する方法
を解説します。


1. 基本コマンド

Git で追跡済みファイルを削除する場合は次のコマンドを使います。

git rm ファイル名

フォルダの場合

git rm -r フォルダ名

2. 削除の流れ(基本)

① ファイル削除

git rm sample.txt

② コミット

git commit -m "不要ファイル削除"

③ GitHubへ反映

git push

これで

  • ローカル
  • GitHub

両方から削除されます。


3. フォルダ削除

フォルダの場合は -r オプションを使用します。

git rm -r sample-folder

その後は同様に

git commit -m "フォルダ削除"
git push

4. .gitignore に追加した後に削除する場合

よくあるケースとして

「一度コミットしたけど .gitignore に追加したい」

という場合があります。

node_modules
.env

この場合は Git の追跡を一度解除する必要があります。

手順

git rm -r --cached node_modules

ポイント

オプション 意味


--cached Git の追跡のみ削除(ローカルファイルは残す)


5. 追跡解除の流れ

git rm -r --cached node_modules
git commit -m "node_modules を追跡解除"
git push

これで

  • ローカルには残る
  • GitHub にはアップされない

状態になります。


6. まとめ

やりたいこと コマンド


ファイル削除 git rm ファイル名
フォルダ削除 git rm -r フォルダ名
Git追跡のみ削除 git rm -r --cached フォルダ名


7. サンプルコード

docker/mysql/data を git の追跡から外したい場合

  • docker/mysql/data の下に、.gitkeep ファイルを作成
cd docker/mysql/data
touch .gitkeep
  • プロジェクトファイル直下に .gitignore ファイルを作成(すでにあればそのまま)

ファイルの中に以下の記述を追記

docker/mysql/data/*
!docker/mysql/data/.gitkeep
  • git 追跡から docker/mysql/data/ を削除
git rm -r --cached docker/mysql/data
  • git commit を実行

8. よくある質問

Q. ローカルだけ削除したい場合は?

普通に削除すればOKです。

rm ファイル名

ただし Git の管理からは消えないので注意してください。


Q. GitHubだけ削除はできる?

基本的には commit + push を使うため同時削除になります。


参考

公式ドキュメント
https://git-scm.com/docs/git-rm

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?