늒네 기록

[git] --global 옵션 설명, 그리고 git이 읽는 config 파일 우선순위 본문

기타 공부 기록/git

[git] --global 옵션 설명, 그리고 git이 읽는 config 파일 우선순위

jaeha lee 2022. 8. 29. 00:17

앞선 글에서 global하게 사용할 user와 alias를 세팅했는데, 그렇다면 특정 프로젝트에서는 디폴트로 설정한 유저와 다른 유저를 사용하고 싶으면 어떻게 할까?

 

앞선 글에서 사용했던 명령어를 다시 살펴보자.

$ git config --global user.name = '사용하고 싶은 이름'

여기에 --global 옵션이 붙어있는 것을 알 수 있다. 이에 대한 자세한 설명은 공식 문서에서 볼 수 있는데,

 

Git - git-config Documentation

When using the deprecated [section.subsection] syntax, changing a value will result in adding a multi-line key instead of a change, if the subsection is given with at least one uppercase character. For example when the config looks like [section.subsection

git-scm.com

 

--global

For writing options: write to global `~/.gitconfig` file rather than the repository `.git/config`, write to `$XDG_CONFIG_HOME/git/config` file if this file exists and the `~/.gitconfig` file doesn’t.

For reading options: read only from global `~/.gitconfig` and from `$XDG_CONFIG_HOME/git/config` rather than from all available files.

 

설명을 러프하게 요약하면, global 옵션을 붙인 경우, ~/.gitconfig 경로에 내용을 저장하고, 그렇지 않으면 해당 repository의 .git/config 경로에 내용을 저장한다고 되어있다. 그런데 그렇다면, 만약 global 옵션을 붙인 상태에서 값을 저장하고, 붙이지 않은 상태에서도 값을 저장한다면, 둘 중 어떤 것을 적용하게 될까? 이에 대한 내용도 위의 링크에 같이 언급되어 있는데, FILES라고 되어있는 부분을 읽으면 된다(원문 아래 요약을 하겠다.).

 

FILES

If not set explicitly with --file, there are four files where git config will search for configuration options:

$(prefix)/etc/gitconfig
    System-wide configuration file.

$XDG_CONFIG_HOME/git/config
    Second user-specific configuration file. If $XDG_CONFIG_HOME is not set or empty, $HOME/.config/git/config will be used. Any single-valued variable set in this file will be overwritten by whatever is in ~/.gitconfig. It is a good idea not to create this file if you sometimes use older versions of Git, as support for this file was added fairly recently.

~/.gitconfig
    User-specific configuration file. Also called "global" configuration file.

$GIT_DIR/config
    Repository specific configuration file.

$GIT_DIR/config.worktree
    This is optional and is only searched when extensions.worktreeConfig is present in $GIT_DIR/config.

If no further options are given, all reading options will read all of these files that are available. If the global or the system-wide configuration file are not available they will be ignored. If the repository configuration file is not available or readable, git config will exit with a non-zero error code. However, in neither case will an error message be issued.

The files are read in the order given above, with last value found taking precedence over values read earlier. When multiple values are taken then all values of a key from all files will be used.

 

요약하자면, git이 config를 읽을 수 있는 위치는 볼드로 표시한 위의 다섯가지가 있고, 위에 언급된 위치부터 순서대로 읽고 나서 제일 마지막에 발견된 config를 사용한다고 되어있다. 즉, global을 붙여서 저장해놓은 값(위에서 3번째 위치)과 로컬에 받아온 repo의 경로에서 global 옵션 안 붙이고 저장한 값(.git/config에 저장됨, 위에서 4번째 위치)이 있다면, 더 아래에 있는 .git/config에서 가져온 값을 사용하게 된다.

 

이를 활용하면 특정 repo에만 적용되는 로컬룰 같은 것을 만들 수 있겠다.

반응형
Comments