개발/Git

gitignore 사용법

아르카눔 2024. 10. 29. 13:41



1. .gitignore 파일 생성:

# 프로젝트 루트 디렉토리에서
touch .gitignore

 

혹은 다른 방법으로 생성

 



2. 기본적인 패턴 규칙:

 

.gitignore 파일 내부에 작성 



2.1. 특정 파일 무시

 

example.txt

2.2. 특정 확장자 무시

 

*.log
*.temp
*.tmp

2.3. 특정 디렉토리 무시

 

node_modules/
dist/
build/

2.4. 특정 경로의 파일/디렉토리 무시

 

logs/debug.log
build/output/

2.5. 예외 처리 (무시하지 않을 파일 지정)

 

!important.log

2.6. 디렉토리 내의 모든 것을 무시하되 디렉토리는 유지

 

logs/*

 

3. 자주 사용되는 무시 패턴:



3.1. 운영체제 파일

 

.DS_Store       # Mac
Thumbs.db       # Windows
desktop.ini     # Windows

3.2. IDE/편집기 설정

 

.idea/          # IntelliJ
.vscode/        # Visual Studio Code
*.swp           # Vim
*.swo           # Vim

3.3. 의존성 디렉토리

 

node_modules/   # Node.js
vendor/         # PHP/Composer
venv/           # Python

3.4. 빌드 출력

 

dist/
build/
*.class        # Java
*.pyc          # Python

 

4. 이미 추적 중인 파일을 무시하려면:

 

4.1. 캐시에서 파일 제거

 

git rm --cached filename

4.2. 캐시에서 디렉토리 제거 

 

git rm -r --cached directory/


유용한 팁:

 

- '#'으로 시작하는 줄은 주석으로 처리
- 빈 줄은 무시
- '/' 로 시작하면 하위 디렉토리에는 적용되지 않음
- '/' 로 끝나면 디렉토리만 매치
- '*' 는 0개 이상의 문자를 매치
- '?' 는 1개의 문자를 매치
- '**' 는 중첩된 디렉토리를 매치


Github의 gitignore repo: 링크

 

 

 

 

References:

https://wikidocs.net/277237

https://www.toptal.com/developers/gitignore

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

https://docs.github.com/ko/get-started/git-basics/ignoring-files

https://github.com/github/gitignore