본문 바로가기

[Git]

[Git] .gitignore 파일

728x90
반응형

.gitignore 파일

Git Repository나 Staging Area에 추가되지 않는 폴더/파일을 정의하는 파일이다.

즉 Git 버전 관리에서 제외할 파일의 목록을 저장하는 파일이다.

이때 .gitignore 파일은 Git의 루트(root) 디렉터리에 저장한다.

 

.gitignore에 정의된 파일/디렉터리는 Staging Area에서 제외되므로 Git 버전관리에서 Tracking 되지 않는다.

프로젝트를 Git으로 관리할 때 Git으로 관리할 필요가 없는 특정 파일이나 디렉터리 목록을 정의 해놓고 관리할 수 있다.

 

디렉터리 내용물 무시하기

[directory name]/

예) logs/

 

특정 확장자 파일 무시하기

*.[확장자]

예) *.log

 

패턴

'#'로 시작하는 라인 주석처리
표준 Glob 패턴 사용
슬래시(/)로 시작하면 현재 디렉터리만 적용
디렉터리는 슬래시(/)를 끝에 사용
느낌표(!)로 시작하는 패턴은 트랙킹에 포함

 

 

대표적인 예

  • NodeJS-Express로 개발할 때 npm module
  • Java 컴파일된(.class) 파일
  • AWS 비밀 키, JWT 비밀 키
  • python 실행 후 생성된 __pycache__ 파일
  • 각종 config 파일 (config.toml, config.xml, config.json 등)
  • 디버깅 용도로 생성한 .log 파일
# ignore all .class files
*.class

# exclude lib.class from "*.class", meaning all lib.class are still tracked
!lib.class

# ignore all json files whose name begin with 'temp-'
temp-*.json

# only ignore the build.log file in current directory, not those in its subdirectories
/build.log

# specify a folder with slash in the end
# ignore all files in any directory named temp
temp/

# ignore doc/notes.txt, but not doc/server/arch.txt
bin/*.txt

# ignore all .pdf files in the doc/ directory and any of its subdirectories
# /** matches 0 or more directories
doc/**/*.pdf

 

참고하면 좋은 사이트

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

 

gitignore.io

Create useful .gitignore files for your project

www.toptal.com

위의 사이트에서 운영체제, 개발 환경(IDE), 프로그래밍 언어를 검색하면 적당한 .gitignore 파일을 생성해준다.

일반적인 .gitignore 파일이 생성되므로 자신의 환경/프로젝트에 맞춰 내용을 수정하면 된다.

 

 

주의점

이미 버전관리에 포함된 파일(Staging Area/Repository commit)은 포함된 이후에 .gitignore 에 추가한다고 제외되지 않는다.

Git에 올라간 파일은 이미 추적(Track) 대상이기 때문이다.

 

이 경우에는 아래 커맨드로 원격 저장소의 파일을 모두 제거한 뒤 다시 push 한다.

git rm -r --cached .
git add. 
git commit -m "{commit_message}"
git push origin {branch}

 

또는 파일/디렉터리를 제거해야 한다.

git rm [file/directory]

 


Reference

반응형

'[Git]' 카테고리의 다른 글

[Git] GitHub Copilot Chat 전체 프로프트 유출  (0) 2023.05.23
[Git] Gitmoji  (0) 2023.05.05
[Git] commit message convention  (0) 2023.04.23