Server&DevOps/Git

git 특정 파일 history 제거하는 방법

킨글 2025. 3. 9. 15:29

.env 파일의 경우 push한 이력이 있으면 계정, 비밀번호 등이 작성되어 있기 때문에 

아예 history를 지우고 싶은 경우가 생긴다.

 

이 경우에 아래 명령어를 사용하면 해당 파일만 명령어를 제거할 수 있다.

원리는 history를 모두 다시 작성하는 방식으로 진행된다.

 

.test 파일과 .test2 파일을 함께 push한 history의 경우 .test 파일의 history만 지워지게 된다. 

 

git filter-branch --force --index-filter "git rm --cached --ignore-unmatch .test" --prune-empty --tag-name-filter cat -- --all
git push origin --force --all

 

실행한 사진