피드로 돌아가기
Putting a file in .gitignore does nothing if git already tracks it. I built a CLI to find the leftovers.
Dev.toDev.to
DevOps

Git Index 조작을 통한 Tracked-Ignored 파일 탐색 및 자동 제거

Putting a file in .gitignore does nothing if git already tracks it. I built a CLI to find the leftovers.

benjamin2026년 6월 19일3intermediate

Context

.gitignore 설정 전 이미 Commit된 파일이 Index에 남아 계속 Tracking되는 Git의 기본 동작으로 인한 보안 취약점 발생. 단순 Grep 방식으로는 Negation rule 및 Nested .gitignore 등 복잡한 Matching Semantics를 정확히 처리하기 어려운 한계 존재.

Technical Solution

  • git ls-files -i -c --exclude-standard 명령어를 활용하여 Git 엔진이 직접 판별한 Tracked-Ignored 파일 셋을 정확하게 추출하는 설계
  • git check-ignore의 Short-circuiting 문제(이미 Tracking 중인 파일은 Ignored로 인식하지 않음) 해결을 위해 GIT_INDEX_FILE 환경 변수를 빈 경로로 설정
  • 가상의 Empty Index를 참조하게 함으로써 Git이 모든 파일을 Untracked 상태로 인식하게 하여 매칭된 구체적 Rule과 Line 번호를 확보하는 트릭 적용
  • git rm --cached 명령어를 통한 로컬 파일 유지 및 Index 내 추적 상태만 제거하는 안전한 자동화 로직 구현
  • Node.js 및 Python Standard Library만 사용하여 의존성을 제거한 Zero-dependency CLI 구조 설계
  • CI Gate 연동을 위해 탐색 결과 존재 시 Exit Code 1을 반환하는 Fail-fast 메커니즘 도입

- .gitignore 업데이트 후 `git rm --cached`를 통해 기존 추적 파일의 인덱스 제거 여부 확인 - 복잡한 Matching Logic을 직접 구현하기보다 도메인 도구(Git)의 Native 명령어를 조합하여 정확성 확보 - CI 파이프라인에 Ignored-tracked 파일 검사 단계를 추가하여 Secret 유출 및 불필요한 Binary 포함 방지

원문 읽기