1 .gitignore란?
.gitignore는 Git이 무시할 파일 목록을 정의하는 파일이다. 여기에 등록된 파일은 git add .을 해도 스테이징되지 않는다.
무시해야 하는 파일 유형:
- 빌드 결과물 (컴파일된 파일, 번들 등)
- 의존성 디렉토리 (
node_modules/,.venv/) - 환경 설정 파일 (
.env, API 키) - IDE 설정 파일 (
.vscode/,.idea/) - OS 생성 파일 (
.DS_Store,Thumbs.db) - 로그 파일 (
*.log)
2 패턴 문법
2.1 기본 규칙
2.2 와일드카드
| 패턴 | 의미 | 예시 |
|---|---|---|
* |
슬래시를 제외한 모든 문자 | *.log → 모든 .log 파일 |
** |
모든 디렉토리 (0개 이상) | **/test/ → 모든 깊이의 test 폴더 |
? |
아무 한 글자 | file?.txt → file1.txt, fileA.txt |
[abc] |
a, b, c 중 하나 | file[123].txt → file1.txt, file2.txt |
[0-9] |
범위 내 한 글자 | log[0-9].txt → log0.txt ~ log9.txt |
2.3 디렉토리 지정
2.4 패턴 예시
3 언어/프레임워크별 템플릿
3.1 Python
# Byte-compiled
__pycache__/
*.py[cod]
*$py.class
# Virtual environment
.venv/
venv/
env/
# Distribution
dist/
build/
*.egg-info/
*.egg
# IDE
.idea/
.vscode/
*.swp
# Environment
.env
.env.local
# Jupyter Notebook checkpoints
.ipynb_checkpoints/
# pytest
.pytest_cache/
.coverage
htmlcov/
# mypy
.mypy_cache/3.2 JavaScript / TypeScript
3.3 Java / Kotlin
# Compiled class files
*.class
# Build
build/
target/
out/
# IDE
.idea/
*.iml
.eclipse/
.settings/
# Gradle
.gradle/
gradle-app.setting
# Maven
*.jar
*.war
*.ear
# Log
*.logGitHub에서 제공하는 gitignore 템플릿 저장소에 거의 모든 언어/프레임워크의 .gitignore 템플릿이 있다. 프로젝트 시작 시 참고하면 좋다.
4 이미 추적 중인 파일 무시하기
.gitignore에 추가해도 이미 Git이 추적하고 있는 파일은 계속 추적된다. 추적을 중단하려면 명시적으로 제거해야 한다.
rm 'secret.env'
rm 'node_modules/.package-lock.json'
rm 'node_modules/express/package.json'
rm 'node_modules/express/index.js'
...
파일이 많을수록 rm '...' 줄이 많이 출력된다. --cached 는 로컬 파일을 삭제하지 않고 Git 추적에서만 제거한다.
# .gitignore에 추가
echo "secret.env" >> .gitignore
# 변경사항 커밋
git add .gitignore
git commit -m "chore: Remove tracked files and update .gitignore"[main 3b2a1c0] chore: Remove tracked files and update .gitignore
2 files changed, 1 insertion(+)
delete mode 100644 secret.env
커밋 로그에 delete mode 100644 secret.env 가 나타난다. 이후 .gitignore 에 등록된 파일은 더 이상 추적되지 않는다.
git rm --cached는 로컬 파일을 삭제하지 않지만, 다른 팀원이 git pull을 하면 해당 파일이 삭제될 수 있다. 팀에 미리 공유하고 진행해야 한다.
5 글로벌 .gitignore
모든 Git 저장소에 공통으로 적용되는 gitignore를 설정할 수 있다. OS나 IDE 관련 파일에 적합하다.
~/.gitignore_global 내용:
# macOS
.DS_Store
.AppleDouble
.LSOverride
# Windows
Thumbs.db
ehthumbs.db
Desktop.ini
# Linux
*~
.directory
# IDE
.idea/
.vscode/
*.swp
*.swo
*~
# Editor backup files
*.bak
*.orig이렇게 하면 프로젝트별 .gitignore에 OS/IDE 설정을 넣지 않아도 된다.
6 .gitkeep: 빈 디렉토리 추적
Git은 빈 디렉토리를 추적하지 않는다. 빈 디렉토리 구조를 유지하고 싶다면 .gitkeep이라는 빈 파일을 넣는 관례가 있다.
# 빈 디렉토리에 .gitkeep 생성
touch logs/.gitkeep
touch uploads/.gitkeep
# .gitignore에서 디렉토리 내용은 무시하되 .gitkeep은 유지
logs/*
!logs/.gitkeep.gitkeep은 Git의 공식 기능이 아니라 커뮤니티 관례다. 파일 이름은 아무거나 가능하지만 .gitkeep이 가장 널리 사용된다.
7 디버깅: 왜 파일이 무시되지 않는가?
.gitignore:5:*.local.json src/config.local.json
파일:줄번호:매칭된 패턴 형식으로 출력된다. .gitignore 5번째 줄의 *.local.json 패턴에 매칭됐다는 의미이다.
파일이 무시되지 않는다면 (이미 추적 중이거나 규칙이 없는 경우) 아무 출력도 없다:
이 경우 git status 에서 해당 파일이 여전히 modified 또는 untracked 로 표시된다. 이미 추적 중이라면 git rm --cached 로 추적에서 제거해야 한다.
8 요약
| 상황 | 방법 |
|---|---|
| 새 프로젝트 시작 | GitHub 템플릿 기반으로 .gitignore 생성 |
| 이미 추적 중인 파일 무시 | git rm --cached + .gitignore 추가 |
| OS/IDE 파일 무시 | 글로벌 .gitignore 설정 |
| 빈 디렉토리 유지 | .gitkeep 파일 추가 |
| 규칙 디버깅 | git check-ignore -v |