02-04 .gitignore와 파일 관리

무엇을 추적하지 말지, 줄바꿈·바이너리를 어떻게 다룰지 — .gitignore와 .gitattributes.


무엇을 추적하지 말지, 줄바꿈·바이너리를 어떻게 다룰지 — .gitignore와 .gitattributes.

목표: 저장소를 깔끔하게 유지하고 OS 간 줄바꿈 문제 예방


🚫 .gitignore

추적하지 않을 파일 패턴을 지정한다.

# 의존성
node_modules/
__pycache__/
vendor/

# 빌드 산출물
dist/
build/
*.o
*.class

# 로그·임시
*.log
*.tmp
.cache/

# 환경·비밀 (절대 커밋 금지!)
.env
.env.*
*.key
*.pem
credentials.json

# OS / 에디터
.DS_Store
Thumbs.db
.idea/
.vscode/

# 예외: 무시하지 않음
!.vscode/settings.json

패턴 규칙

패턴의미
file.txt모든 위치의 해당 파일
/file.txt루트의 해당 파일만
dir/디렉토리 전체
*.log확장자 매칭
!important.log예외(무시 해제)
**/temp모든 깊이의 temp
build/*.obuild 안의 .o만

⚠️ 이미 추적 중인 파일 무시하기

.gitignore추적 안 된 파일에만 적용된다. 이미 커밋된 파일은 캐시에서 빼야 한다.

git rm --cached file.txt          # 추적만 해제(파일 유지)
git rm -r --cached node_modules   # 디렉토리
git commit -m "chore: gitignore 적용"

🔍 무시 규칙 디버깅

git check-ignore -v file.txt   # 어떤 규칙이 무시하는지(파일:줄)
git status --ignored           # 무시된 파일까지 표시

🌍 전역 gitignore

OS/에디터 파일은 개인 전역 설정으로 (팀 .gitignore를 더럽히지 않음):

git config --global core.excludesfile ~/.gitignore_global
# ~/.gitignore_global 에 .DS_Store, *.swp 등

📐 .gitattributes — 파일별 동작 지정

줄바꿈 정규화 (OS 간 충돌 예방)

* text=auto                 # 자동 정규화(저장소엔 LF)
*.sh text eol=lf            # 셸 스크립트는 항상 LF
*.bat text eol=crlf         # 배치는 CRLF
*.png binary                # 바이너리(diff/정규화 안 함)

[!tip] autocrlf vs .gitattributes core.autocrlf개인 설정이라 팀원마다 다를 수 있다. .gitattributes저장소에 커밋되어 모두에게 동일 적용되므로 더 안전하다.

diff·머지 동작

package-lock.json -diff          # diff에서 생략(노이즈 감소)
*.md merge=union                 # 양쪽 변경 모두 유지

🧹 줄바꿈 정규화 후 재적용

# .gitattributes 추가/변경 후 전체 재정규화
git add --renormalize .
git commit -m "chore: 줄바꿈 정규화"

📋 체크리스트

  • 언어/도구별 .gitignore 작성
  • 비밀 파일(.env, *.key) 무시 확인
  • rm --cached로 추적 해제
  • check-ignore로 규칙 디버깅
  • 전역 gitignore로 OS 파일 분리
  • .gitattributes로 줄바꿈 정규화
  • autocrlf vs gitattributes 차이

🔗 관련 노트

  • 02-03-커밋-컨벤션과-시맨틱-버저닝 — 이전
  • 02-05-stash-tag-cherry-pick — 다음
  • 03-03-모노레포와-대용량-관리 — LFS·바이너리
  • 01-01-Git-개념과-동작원리 — autocrlf 설정

마지막 업데이트: 2026-06-02