#方法
.gitignoreへgit管理上無視したいファイルやディレクトリを指定。
echo '対象にしたいディレクトリやファイルのpath' >> .gitignore
ファイルやディレクトリをgit管理対象から除外する
$ git rm --cached ファイル名 # 指定ファイルをgit管理対象から削除
$ git rm -r --cached ディレクトリ名/ # 指定ディレクトリをgit管理対象から除外
--cached
オプションをつけると、対象ファイル/ディレクトリ を残したまま、gitの管理対象から除外。
--cached
オプションをなしだと、対象ファイル/ディレクトリ を削除した上、gitの管理対象からも除外。
参考:
具体例
前提:カレントディレクトリで、rails new してcommit 済み
./.gitignore
+ /test/
$ git rm -r --cached .
=>
rm '.gitignore'
rm '.ruby-version'
rm 'Gemfile'
rm 'Gemfile.lock'
rm 'README.md'
rm 'Rakefile'
rm 'app/assets/config/manifest.js'
rm 'app/assets/images/.keep'
rm 'app/assets/javascripts/application.js'
rm 'app/assets/javascripts/cable.js'
rm 'app/assets/javascripts/channels/.keep'
rm 'app/assets/stylesheets/application.css'
rm 'app/channels/application_cable/channel.rb'
rm 'app/channels/application_cable/connection.rb'
rm 'app/controllers/application_controller.rb'
rm 'app/controllers/concerns/.keep'
rm 'app/helpers/application_helper.rb'
rm 'app/jobs/application_job.rb'
rm 'app/mailers/application_mailer.rb'
rm 'app/models/application_record.rb'
rm 'app/models/concerns/.keep'
rm 'app/views/layouts/application.html.erb'
rm 'app/views/layouts/mailer.html.erb'
rm 'app/views/layouts/mailer.text.erb'
rm 'bin/bundle'
rm 'bin/rails'
rm 'bin/rake'
rm 'bin/setup'
rm 'bin/spring'
rm 'bin/update'
rm 'bin/yarn'
rm 'config.ru'
rm 'config/application.rb'
rm 'config/boot.rb'
rm 'config/cable.yml'
rm 'config/credentials.yml.enc'
rm 'config/database.yml'
rm 'config/environment.rb'
rm 'config/environments/development.rb'
rm 'config/environments/production.rb'
rm 'config/environments/test.rb'
rm 'config/initializers/application_controller_renderer.rb'
rm 'config/initializers/assets.rb'
rm 'config/initializers/backtrace_silencers.rb'
rm 'config/initializers/content_security_policy.rb'
rm 'config/initializers/cookies_serializer.rb'
rm 'config/initializers/filter_parameter_logging.rb'
rm 'config/initializers/inflections.rb'
rm 'config/initializers/mime_types.rb'
rm 'config/initializers/wrap_parameters.rb'
rm 'config/locales/en.yml'
rm 'config/puma.rb'
rm 'config/routes.rb'
rm 'config/spring.rb'
rm 'config/storage.yml'
rm 'db/seeds.rb'
rm 'lib/assets/.keep'
rm 'lib/tasks/.keep'
rm 'log/.keep'
rm 'package.json'
rm 'public/404.html'
rm 'public/422.html'
rm 'public/500.html'
rm 'public/apple-touch-icon-precomposed.png'
rm 'public/apple-touch-icon.png'
rm 'public/favicon.ico'
rm 'public/robots.txt'
rm 'storage/.keep'
rm 'test/application_system_test_case.rb'
rm 'test/controllers/.keep'
rm 'test/fixtures/.keep'
rm 'test/fixtures/files/.keep'
rm 'test/helpers/.keep'
rm 'test/integration/.keep'
rm 'test/mailers/.keep'
rm 'test/models/.keep'
rm 'test/system/.keep'
rm 'tmp/.keep'
rm 'vendor/.keep'
$ git status
=>
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
deleted: .gitignore
deleted: .ruby-version
deleted: Gemfile
deleted: Gemfile.lock
deleted: README.md
deleted: Rakefile
deleted: app/assets/config/manifest.js
deleted: app/assets/images/.keep
deleted: app/assets/javascripts/application.js
deleted: app/assets/javascripts/cable.js
deleted: app/assets/javascripts/channels/.keep
deleted: app/assets/stylesheets/application.css
deleted: app/channels/application_cable/channel.rb
deleted: app/channels/application_cable/connection.rb
deleted: app/controllers/application_controller.rb
deleted: app/controllers/concerns/.keep
deleted: app/helpers/application_helper.rb
deleted: app/jobs/application_job.rb
deleted: app/mailers/application_mailer.rb
deleted: app/models/application_record.rb
deleted: app/models/concerns/.keep
deleted: app/views/layouts/application.html.erb
deleted: app/views/layouts/mailer.html.erb
deleted: app/views/layouts/mailer.text.erb
deleted: bin/bundle
deleted: bin/rails
deleted: bin/rake
deleted: bin/setup
deleted: bin/spring
deleted: bin/update
deleted: bin/yarn
deleted: config.ru
deleted: config/application.rb
deleted: config/boot.rb
deleted: config/cable.yml
deleted: config/credentials.yml.enc
deleted: config/database.yml
deleted: config/environment.rb
deleted: config/environments/development.rb
deleted: config/environments/production.rb
deleted: config/environments/test.rb
deleted: config/initializers/application_controller_renderer.rb
deleted: config/initializers/assets.rb
deleted: config/initializers/backtrace_silencers.rb
deleted: config/initializers/content_security_policy.rb
deleted: config/initializers/cookies_serializer.rb
deleted: config/initializers/filter_parameter_logging.rb
deleted: config/initializers/inflections.rb
deleted: config/initializers/mime_types.rb
deleted: config/initializers/wrap_parameters.rb
deleted: config/locales/en.yml
deleted: config/puma.rb
deleted: config/routes.rb
deleted: config/spring.rb
deleted: config/storage.yml
deleted: db/seeds.rb
deleted: lib/assets/.keep
deleted: lib/tasks/.keep
deleted: log/.keep
deleted: package.json
deleted: public/404.html
deleted: public/422.html
deleted: public/500.html
deleted: public/apple-touch-icon-precomposed.png
deleted: public/apple-touch-icon.png
deleted: public/favicon.ico
deleted: public/robots.txt
deleted: storage/.keep
deleted: test/application_system_test_case.rb
deleted: test/controllers/.keep
deleted: test/fixtures/.keep
deleted: test/fixtures/files/.keep
deleted: test/helpers/.keep
deleted: test/integration/.keep
deleted: test/mailers/.keep
deleted: test/models/.keep
deleted: test/system/.keep
deleted: test/test_helper.rb
deleted: tmp/.keep
deleted: vendor/.keep
$ git add .
$ git status
=>
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: .gitignore
deleted: test/application_system_test_case.rb
deleted: test/controllers/.keep
deleted: test/fixtures/.keep
deleted: test/fixtures/files/.keep
deleted: test/helpers/.keep
deleted: test/integration/.keep
deleted: test/mailers/.keep
deleted: test/models/.keep
deleted: test/system/.keep
deleted: test/test_helper.rb
$ git commit -m "~~~"
# remote repository 作成
$ git remote add origin git@~~.git
$ git push origin branch_name
今回 .gitignore
で指定した testディレクトリ 配下のファイル群がリモートリポジトリで削除扱いになっている◎