LoginSignup
2
3

More than 1 year has passed since last update.

GitHubで管理したくないディレクトリを間違えてGitHubにあげてしまった!そんな時の対処方法

Last updated at Posted at 2023-01-14

【はじめに】

GitHubで管理したくないファイルを間違えてGitHubにあげてしまった!
そんな時の対処法について備忘録も兼ね、node_modulesを例に簡単にまとめておきます。

【手順】

  1. プロジェクトのルートディレクトリに.gitignoreを作成する
  2. .gitignoreにGitHubで管理したくないディレクトリを追加する
  3. 対象のディレクトリをgitの追跡対象から外す
  4. .gitignoreをGitHubにpushする

1. プロジェクトのルートディレクトリに.gitignoreを作成する

$ cd プロジェクトのルートディレクトリ
$ touch .gitignore

.gitignore作成後のディレクトリ構成例

├scr
├node_modules
.gitignore

2. .gitignoreにGitHubで管理したくないディレクトリを追加する

.gitignore
# GitHubで管理したくないディレクトリをここに追加する

node_modules/

3. 対象のディレクトリをgitの追跡対象から外す

$ git rm -r --cache [追跡対象から外したいディレクトリ]
$ git rm -r --cache node_module

4. .gitignoreをGitHubにpushする

$ git add .
$ git commit -m "[commit message]"
$ git push origin [branch_name]

最後にGitHubに.gitignoreをpushすればGitHubからnode_moduleが削除されます。
また、今後もgitの管理下から外れます。

他にもGitHubで管理したくないディレクトリがある場合、上記と同じ手順で対照することが可能です。

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