개발/Git

[Git] .gitignore를 설정해서 불필요한 파일 무시하기

growing-dev 2023. 2. 6. 23:20
반응형

우리가 git repository를 사용해서 commit 하고 push 하는 식으로 코드를 관리할 때, commit 하거나 push 하고 싶지 않은 파일이 있을 수 있다. 예를 들면 IDE에서 로컬에 파일을 생성해서 캐시 해두고 빌드나 각종 환경설정을 위해 사용하는 파일들 같은 경우는 사람마다 환경이 다를 수도 있고 또 사용하는 IDE가 다를 수도 있다. 이런 경우에는 git commit/push 하는 경우에 해당 파일들을 제거해서 commit/push 해야 한다. 한두 번이야 일일이 내가 제거하면 되지만 여러 번 반복되는 작업을 할 때에 굉장히 비효율적이고 불편하다. 그래서 필요한 파일이 바로. gitignore이다.

 

. gitignore를 설정해서 불필요한 파일 무시하기

. gitignore는 파일명에서 알 수 있듯이 무시하고자 하는 파일이나 디렉터리들을 나열해 놓은 파일이다. git은 해당. gitignore 파일에 나열된 파일 혹은 디렉터리를 git add 할 때 무시하도록 동작한다. 우리는 . gitignore 파일을 활용해서 add/commit/push 하고 싶지 않은 파일이나 디렉토리를 git이 무시하도록 설정할 수 있다. 그리고 그 설정을 원격 repository에 함께 push 해 놓으면 같이 개발하는 개발자들도 동일하게 무시할 수 있게 되므로 불필요한 작업을 하지 않아도 되어서 좋다.

 

 

.gitignore 작성 방법

아래 사이트는 gitignore 파일을 쉽게 생성할 수 있도록 도와주는 사이트이다.

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

 

gitignore.io

Create useful .gitignore files for your project

www.toptal.com

해당 사이트에서 내가 사용하는 개발 환경이나 언어를 입력하면 그에 맞는 gitignore 파일의 내용을 생성해 준다.

내 경우는 주로 CMake, C++, VisualStudioCode를 사용하기 때문에 그렇게 입력했다.

아래와 같은 파일이 생성되었다.

# Created by https://www.toptal.com/developers/gitignore/api/cmake,c++,visualstudiocode
# Edit at https://www.toptal.com/developers/gitignore?templates=cmake,c++,visualstudiocode

### C++ ###
# Prerequisites
*.d

# Compiled Object files
*.slo
*.lo
*.o
*.obj

# Precompiled Headers
*.gch
*.pch

# Compiled Dynamic libraries
*.so
*.dylib
*.dll

# Fortran module files
*.mod
*.smod

# Compiled Static libraries
*.lai
*.la
*.a
*.lib

# Executables
*.exe
*.out
*.app

### CMake ###
CMakeLists.txt.user
CMakeCache.txt
CMakeFiles
CMakeScripts
Testing
Makefile
cmake_install.cmake
install_manifest.txt
compile_commands.json
CTestTestfile.cmake
_deps

### CMake Patch ###
# External projects
*-prefix/

### VisualStudioCode ###
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
!.vscode/*.code-snippets

# Local History for Visual Studio Code
.history/

# Built Visual Studio Code Extensions
*.vsix

### VisualStudioCode Patch ###
# Ignore all local history of files
.history
.ionide

# End of https://www.toptal.com/developers/gitignore/api/cmake,c++,visualstudiocode

 

다음은 KitWare/CMake github에서 가져온. gitignore 파일이다.

# Common build directories
build*/

# Exclude MacOS Finder files.
.DS_Store

*.user*

*.pyc

Help/_generated
Testing
CMakeUserPresets.json

# Visual Studio work directory
.vs/
# Visual Studio build directory
out/

# Visual Studio Code
.vscode/
.cache/

# CLion work directory
.idea/
# CLion build directories
cmake-build-*/

 

 

. gitignore에 포함되는 종류

 

일반적으로. gitignore에 포함되는 종류는 크게 3가지로 볼 수 있다.

  • 빌드 후 나오는 결과물
  • IDE를 사용하면서 나오는 설정 파일과 캐시들
  • 각 로컬 개발환경에 필요한 각종 설정들

 

 

. gitignore 작성 규칙

작성 규칙은 특별한 문법이 있지 않고 심플하며 다음과 같다.

  • #으로 주석을 표시한다.
  • 표준 glob를 활용할 수 있으며 *. obj 등과 같이 활용할 수 있다.
  • 제외할 파일 이름을 직접 명시할 수 있다.
  • 제외할 디렉터리를 명시할 수 있고 끝에 / 를 붙인다.

 

 

결론

. gitignore를 잘 활용해서 불필요한 파일이 repository에 들어가는 일이 없어야 하겠다. 하지만 만약 필요한 파일이 추가되어야 하는데 무시된다면 혼란이 발생할 수 있으므로 활용할 때 주의가 필요할 것이다.

반응형