Git에 .gitignore 파일 적용

gitignore.io 를 이용해 java 프로젝트에 .gitignore 파일을 생성하고 이를 Git에 적용한다.

Posted by Seoyoung Lee on January 14, 2021 · 2 mins read

.gitignore 란?

github 또는 gitlab 등 Git을 이용하다보면 .gitignore라는 파일을 쉽게 볼 수 있다.

.gitignore란 깃에서 특정 파일이나 디렉토리를 관리대상에서 제외할 때 생성하는 파일이다.

프로젝트의 Backup file, Log file들을 주로 제외시키며 .gitignore를 사용해 이런 파일들을 untracked files로 설정해 Git이 이 파일들을 track하지 않도록 한다.

.gitignore 파일 만들기

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

직접 작성할 수도 있지만 gitignore.io (click here!) 사이트에서 쉽게 작성할 수 있다.

위 URL을 통해 사이트에 들어가면, 운영체제, 개발환경(IDE), 프로그래밍 언어를 입력해 .gitignore 파일을 생성해주는 검색창이 나타난다. 운영체제, 개발환경, 개발 언어에 따라 생성되는 파일이 다르므로 내 프로젝트에 적합한 .gitignore 파일을 만들기 위해서는 이들을 입력해야한다.

나는 Java언어를 사용해 Git에 프로젝트를 생성할 것이기 때문에 Java, Eclipse, 그리고 내가 사용하는 운영체제인 MacOS를 입력하여 .gitignore파일을 생성하였다.

결과로 아래와 같은 텍스트들이 화면에 나온다.

# Created by https://www.toptal.com/developers/gitignore/api/macos,java,eclipse
# Edit at https://www.toptal.com/developers/gitignore?templates=macos,java,eclipse

### Eclipse ###
.metadata
bin/
tmp/
*.tmp
*.bak
*.swp
*~.nib
local.properties
.settings/
.loadpath
.recommenders

# External tool builders
.externalToolBuilders/

# Locally stored "Eclipse launch configurations"
*.launch

# PyDev specific (Python IDE for Eclipse)
*.pydevproject

# CDT-specific (C/C++ Development Tooling)
.cproject

# CDT- autotools
.autotools

# Java annotation processor (APT)
.factorypath

# PDT-specific (PHP Development Tools)
.buildpath

# sbteclipse plugin
.target

# Tern plugin
.tern-project

# TeXlipse plugin
.texlipse

# STS (Spring Tool Suite)
.springBeans

# Code Recommenders
.recommenders/

# Annotation Processing
.apt_generated/
.apt_generated_test/

# Scala IDE specific (Scala & Java development for Eclipse)
.cache-main
.scala_dependencies
.worksheet

# Uncomment this line if you wish to ignore the project description file.
# Typically, this file would be tracked if it contains build/dependency configurations:
#.project

### Eclipse Patch ###
# Spring Boot Tooling
.sts4-cache/

### Java ###
# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

### macOS ###
# General
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon


# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

# End of https://www.toptal.com/developers/gitignore/api/macos,java,eclipse

위 내용을 복사하여 프로젝트의 최상위 Directory에 .gitignore라는 파일을 만든 후, 붙여넣기 해주면 끝이다.

이후에 .gitignore 파일을 git에 추가하고 commit 후 push 해주면 해당 프로젝트에 .gitignore 파일이 적용된다.