SERVER/Git

이미 push된 파일 .gitinore 적용하기

완자✨ 2021. 12. 16. 10:56

1. 이미 push된 파일 .gitinore 적용하기

  • git으로 관리하고 싶지 않은 파일들은 .gitignore 에 명시함으로써 해당 파일을 무시할 수 있다.
  • 하지만 종종 무시할 파일을 .gitignore 에 추가하기 전에 git push 해버리는 경우가 있다.
  • 이 때 뒤늦게 .gitignore 을 수정하여 push를 하지만 원격 저장소에서 해당 파일은 사라지지 않는다.

아래의 git 명령들을 실행해주면 .gitignore의 파일들이 적용되어 원격 저장소에서 사라진다.

$ git rm -r --cached .
$ git add .
$ git commit -m "Apply .gitignore"
$ git push

만약

vopgh@DESKTOP-7GBJ0PD MINGW64 /c/WonJin/sparta/homework (master)
$ git rm -r --cached .
error: the following files have staged content different from both the
file and the HEAD:
    homework/.gitignore
    homework/app.py
    homework/secrets.json
(use -f to force removal)

이러한 에러 메세지가 나타난다면 -f 옵션으로 강제 실행이 가능하다.