본문 바로가기
형상관리 - Git

[Git] 기존에 push 된 파일 .gitignore 적용하기

by 마진 2022. 4. 3.

git으로 관리할 필요가 없는 파일을 .gitignore에 표기함으로서 연동대상에서 제외할 수 있다.

하지만, 이미 git으로 관리되고 있는 파일은 .gitignore 에 표기하더라도 제외되지 않는데 기존에 연동된 대상을 연동 목록에서 제외하는 방법은 아래와 같다.

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

 

[명령어 및 옵션]

git rm : working tree와 index에서 파일을 제거

 

-r : 디렉토리를 제거할 때 사용 (하위의 모든 디렉토리까지 제거 - recursive removal)

--cached : index에서만 제거를 해서 unstage하게 처리한다. (연동 제외)

 (--cached 옵션을 사용하지 않으면 디렉토리에서 파일이 완전히 삭제)

 

.(single dot) : working tree를 의미하는 것으로 생각됨.

 

 

 

[참고]

https://cjh5414.github.io/gitignore-update/

 

이미 push된 file .gitignore 적용하기

Jihun's Development Blog

cjh5414.github.io

 

https://git-scm.com/docs/git-rm

 

Git - git-rm Documentation

Remove files matching pathspec from the index, or from the working tree and the index. git rm will not remove a file from just your working directory. (There is no option to remove a file only from the working tree and yet keep it in the index; use /bin/rm

git-scm.com

 

https://stackoverflow.com/questions/572549/difference-between-git-add-a-and-git-add

 

Difference between "git add -A" and "git add ."

The command git add [--all|-A] appears to be identical to git add .. Is this correct? If not, how do they differ?

stackoverflow.com