Git

Git .gitignore 파일 적용

범데이 2022. 4. 4. 01:01

.gitignore이란?

Git에서 관리가 필요하지 않은 파일들을 설정하는 파일이다. 여기서 설정하는 파일들은 git에서 추적하지 않게 된다.

  • 관리하지 않아도 될 Backup File이나, Log File, 컴파일 된 파일들
  • 로컬 개발 환경에 종속적인 파일들
  • 기타 원격 저장소에 실수로 올라가지 않아야 하는 파일들

 

 

.gitignore파일 만들기

  • .gitignore 파일은 항상 프로젝트의 최상위 Directory에 존재해야 한다.

  • 아래의 패턴을 활용하여 git이 untracked할 파일 또는 디렉토리 등을 정의하여 파일을 생성한다.

[예시 파일 내용]

# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

 

[패턴]

작성 패턴은 아래의 규칙을 따른다.
'#'로 시작하는 라인은 무시한다.
표준 Glob 패턴을 사용한다.
슬래시(/)로 시작하면 하위 디렉터리에 적용되지(recursivity) 않는다.
디렉터리는 슬래시(/)를 끝에 사용하는 것으로 표현한다.
느낌표(!)로 시작하는 패턴의 파일은 무시하지 않는다.

 

[문법]

# : comments

# no .a files
*.a

# but do track lib.a, even though you're ignoring .a files above
!lib.a

# only ignore the TODO file in the current directory, not subdir/TODO
/TODO

# ignore all files in the build/ directory
build/

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

# ignore all .pdf files in the doc/ directory
doc/**/*.pdf

 

.gitignore파일 적용하기

적용은 .gitignore 파일을 git에 push하면 된다.

기존에 있던 Project에 .gitignore 파일이 적용이 안되는 경우에는 아래 명령어를 통해 원격 저장소 파일을 제거 후 다시 Push해본다.

git rm -r --cached .
git add .
git commit -m "Apply .gitignore"

 

 

 

.gitignore 관련 유용 사이트

https://www.gitignore.io/ 를 이용하여 원하는 ignore 파일을 생성할 수 있다.

예를들어 위와같이 "Gradle" 항목을 생성하면 다음과 같이  .gitignore파일 내용이 생성된 것을 볼 수 있다. 

# Created by https://www.toptal.com/developers/gitignore/api/gradle
# Edit at https://www.toptal.com/developers/gitignore?templates=gradle

### Gradle ###
.gradle
**/build/
!src/**/build/

# Ignore Gradle GUI config
gradle-app.setting

# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored)
!gradle-wrapper.jar

# Avoid ignore Gradle wrappper properties
!gradle-wrapper.properties

# Cache of project
.gradletasknamecache

# Eclipse Gradle plugin generated files
# Eclipse Core
.project
# JDT-specific (Eclipse Java Development Tools)
.classpath

# End of https://www.toptal.com/developers/gitignore/api/gradle

 

 


#References

https://nesoy.github.io/articles/2017-01/Git-Ignore

 

Git .gitignore 적용하기

 

nesoy.github.io

https://velog.io/@psk84/.gitignore-%EC%A0%81%EC%9A%A9%ED%95%98%EA%B8%B0

 

.gitignore 적용하기

프로젝트 생성후 git을 연동하면서 가장먼저 하게되는 gitignore처리 간략정리

velog.io

 

반응형