Tool/Git

.gitignore로 파일 제외하는 방법 - 생성 및 적용

재은초 2023. 11. 5. 00:01
반응형

.gitignore란?

  • .gitignore 파일이란 Git 버전 관리에서 제외할 파일 목록을 지정하는 파일이다.
  • 예를 들어 Nodejs에서 npm 모듈들은 용량이 크기 때문에 Git에 올리지 않는다.

 

.gitignore 파일 생성

  • git init 을 한 폴더에다가 확장자 없는 .gitignore 파일을 만들어 준다. 
notepad .gitignore               // Window
touch .gitignore                 // Mac

 

.gitignore 사용법

  • .gitignore 파일 안에 아래와 같이 쓰면 된다.

특정 파일 fileName 제외하기

fileName.js

현재 경로에 있는 fileName_1 만 제외하기

/fileName.js

특정 폴더 node_module 안의 파일 다 제외하기

node_modules/

특정 경로의 특정 파일 제외하기

folder/my.txt

특정 경로 아래의 모든 fileName_2 제외하기

folder/**/fileName_2.txt

특정 확장자 파일 다 제외하기

*.txt

예외 만들기

!fileName.txt

 

.gitignore 주의사항

  •  이미 Staging Area나 Repository에 커밋으로 올라간 파일을 gitignore 하기 위해서는 먼저 파일을 제거해야 한다. 즉, git add나 commit이 된 경우에는 .gitignore이 적용되지 않으므로 먼저 해당 단계에서 제거해야 한다.
git rm 파일명
git commit -m "커밋메세지"

 

.gitignore에 알아두면 좋은 사이트

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

 

Reference

반응형