이미 git tracking 대상이 되고 있는 디렉토리나 파일을 commit 후에 .gitignore 대상으로 한다

방법



.gitignore에 git 관리상 무시하고 싶은 파일이나 디렉토리를 지정.


echo '対象にしたいディレクトリやファイルのpath' >> .gitignore

파일 및 디렉토리를 git 관리 대상에서 제외


$ git rm --cached ファイル名 # 指定ファイルをgit管理対象から削除
$ git rm -r --cached ディレクトリ名/ # 指定ディレクトリをgit管理対象から除外
--cached 옵션을 붙이면, 대상 파일/디렉토리를 남긴 채로, git 의 관리 대상으로부터 제외.--cached 옵션이 없으면 대상 파일/디렉토리를 삭제하고 git 관리 대상에서 제외.

참고:



[Git].gitignore의 사양 상세

구체적인 예



전제: 현재 디렉토리에서 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 디렉토리 부하의 파일군이 리모트 리포지토리로 삭제 취급이 되고 있다◎

좋은 웹페이지 즐겨찾기