.gitignore 파일은 git이 추적하지 말아야할 파일/디렉토리 리스트입니다. 어떤 파일을 git에 저장하고 싶지 않을 때 .gitignore에 파일 이름을 추가하시면 됩니다.
1. '.gitignore' 파일이 필요한 이유
빌드 및 프로그램을 실행할 때, git이 관리하는 디렉토리 안에 .access_log라는 디버깅 파일이 추가된다고 가정해보세요.
다음과 같은 이유로 git에 파일을 추가하고 싶지 않을 것입니다.
.access_log파일은 디버깅 로그이고, 같이 작업하는 사용자에게 공유할 필요가 없음..access_log파일은 빌드와 관련이 없고, 프로그램이 실행되면 항상 만들어짐
$ ls -al
total 108
drwxrwxr-x 16 mjs mjs  4096 May 14 21:24 .
drwxrwxr-x 46 mjs mjs  4096 May  6 18:59 ..
-rw-rw-r--  1 mjs mjs     0 May 14 21:24 .access_log
-rw-rw-r--  1 mjs mjs  3169 May  6 18:59 CODE_OF_CONDUCT.md
-rw-rw-r--  1 mjs mjs  1697 May  6 18:59 CONTRIBUTING.md
drwxrwxr-x  9 mjs mjs  4096 May  6 18:59 Crane
drwxrwxr-x  8 mjs mjs  4096 May 14 21:24 .git
....하지만, git 프로젝트 안에 새로운 파일이 추가되면 아래와 같이 Untracked files로 파일이 잡히며, 다른 파일을 git에 추가할 때 매번 이 파일을 제외하고 추가하기가 번거롭습니다.
만약 .gitignore에 .access_log를 등록하면 이 파일을 추적하지 않고, git status 명령어에서도 보이지 않게 됩니다.
$ git status
On branch main
Your branch is up to date with 'origin/main'.
Untracked files:
  (use "git add <file>..." to include in what will be committed)
	.access_log2. '.gitignore' 설정 방법
git의 최상단 디렉토리에 .gitignore 파일을 생성하여 아래와 같이 .access_log를 추가합니다.
- git 최상단 디렉토리는 
.git파일과 동일한 경로 .gitignore에서#은 주석을 의미- 파일 이름을 입력하면 해당 파일을 git에서 무시하겠다는 의미
 
# log files
.access_log위와 같이 .gitignore 파일 설정 후, 다시 git status를 입력해보면 .access_log 파일이 Untracked files에 보이지 않게 됩니다.
$ git status
On branch main
Your branch is up to date with 'origin/main'.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   .gitignore
no changes added to commit (use "git add" and/or "git commit -a").gitignore 패턴
.gitignore에서 아래와 같은 패턴으로 무시하려는 파일 및 폴더를 추가할 수 있습니다.
| Pattern | 이런 파일들을 무시함 | 
|---|---|
| **/logs | logs/debug.log, logs/monday/foo.bar, build/logs/debug.log | 
| **/logs/debug.log | logs/debug.log build/logs/debug.log but not logs/build/debug.log | 
| *.log | debug.log foo.log .log logs/debug.log | 
| *.log !important.log | debug.log trace.log but not important.log logs/important.log | 
| .log !important/.log trace.* | debug.log important/trace.log but not important/debug.log | 
| /debug.log | debug.log but not logs/debug.log | 
| debug.log | debug.log logs/debug.log | 
| debug?.log | debug0.log debugg.log but not debug10.log | 
| debug[0-9].log | debug0.log debug1.log but not debug10.log | 
| debug[01].log | debug0.log debug1.log but not debug2.log debug01.log | 
| debug[!01].log | debug2.log but not debug0.log debug1.log debug01.log | 
| debug[a-z].log | debuga.log debugb.log but not debug1.log | 
| logs | logs logs/debug.log logs/latest/foo.bar build/logs build/logs/debug.log | 
| logs/ | logs/debug.log logs/latest/foo.bar build/logs/foo.bar build/logs/latest/debug.log | 
| logs/ !logs/important.log | logs/debug.log logs/important.log | 
| logs/**/debug.log | logs/debug.log logs/monday/debug.log logs/monday/pm/debug.log | 
| logs/*day/debug.log | logs/monday/debug.log logs/tuesday/debug.log but not logs/latest/debug.log | 
| logs/debug.log | logs/debug.log but not debug.log build/logs/debug.log | 
3. 기본 .gitignore 파일 생성
gitignore.io에서 프로젝트를 입력하면, 그 프로젝트에서 기본적으로 사용하는 .gitignore 파일을 생성해줍니다.
생성 버튼을 누르면 아래와 같은 파일이 생성됩니다. 이 내용을 .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
### Gradle Patch ###
# Java heap dump
*.hprof
# End of https://www.toptal.com/developers/gitignore/api/gradleLoading script...
Related Posts
- Git Alias - 단축/커스텀 명령어 설정
 - Git에서 .gitignore 설정 방법
 - git clone 명령어로 원격 저장소를 로컬에 저장
 - Git - 두개 commit의 차이점 확인 (코드 차이, 파일 차이)
 - Git - 브랜치 이름 변경하는 방법
 - Git - 특정 commit 삭제, 되돌리기 (reset, revert)
 - Git - 리모트 브랜치 생성과 삭제
 - GitHub에 ssh-key 등록 방법 (Windows10)
 - git log - 특정 author의 commit 리스트만 출력
 - git log - commit 리스트 간단하게 출력 (pretty)
 - Git - revert 명령어(commit 되돌리기)
 - git diff 명령어로 파일 변경 내용 확인
 - git show 명령어로 commit 정보 확인
 - Git - 브랜치 생성, 삭제, 체크아웃
 - Git - 원격 저장소(remote) 추가, 해제
 - Git - Staging 영역의 파일을 Untracked로 변경
 - Git - commit log에서 수정된 파일 목록 보기
 - Git - 마지막 commit 수정, 삭제 방법 (reset, amend 명령어)
 - git commit message 에디터 변경 (Vim, Sublime, Atom, VSCode)
 - Git - blame 명령어, 코드 수정한 사람과 Commit 찾기
 - Git - Patch 파일 만들기 & 적용하기