👯

ディレクトリごとに git ユーザーを切り替える

2024/12/03に公開

プロジェクトによって、git ユーザを切り替えたい場合、
.gitconfig をアカウントごとにそれぞれ用意してあげるのがよい。

例えばメインで使うユーザ(main)と、サブで使うユーザ(sub)を切り替えたい場合はこのように作ると、指定のディレクトリ配下はそのユーザで git 管理することができる。

~/.gitconfig
# メインユーザ
[user]
        name = Shumpei
        email = main@example.com

# 任意のディレクトリの時に.gitconfig_subを読み込む
[includeIf "gitdir:~/Documents/workspace/sub-project/"]
        path = ~/.gitconfig_sub
~/.gitconfig_sub
# サブユーザ
[user]
        name = Shumpei.sub
        email = sub@example.com

ポイントは .gitconfig の方に includeIf ディレクティブを用意してあげるところ。

[includeIf "gitdir:~/Documents/workspace/sub-project/"]
        path = ~/.gitconfig_sub

これで自動で切り替えられます。

Discussion