元々、zshrcに以下の様なコマンドを作っていた。
function github_current_user() {
local user_name=`git config user.name`
echo "\ue709:${user_name}"
}
function gitmain() {
git config --global user.name "main_github_username"
git config --global user.email "main_github_email"
source ~/.zshrc
}
function gitsub() {
git config --global user.name "sub_github_username"
git config --global user.email "sub_github_email"
source ~/.zshrc
}
ターミナル上でgitmain
コマンドを実行すると、main_github
のアカウントを使用し、
gitsub
コマンドを実行するとsub_github
のアカウントを使用する様にしていた。
これはこれで便利だったが、
コマンドを実行しない限り、githubアカウントの切り替えが発生しない。
そのため、sub_github
アカウントでcommitすべきリポジトリでmain_github
アカウントでcommitしてしまう。
という事故が発生した。
そこで、.gitconfig
に
[includeIf "gitdir:~/PROJECTS/hogehoge/"]
path = ~/.gitconfig_sub
と追加し、.gitconfig_sub
に
[user]
name = sub_github_username
email = sub_github_email
とした。
これにより、~/PROJECTS/hogehoge/
配下では、自動的にsub_github
アカウントを使用する様になった。
これで、Githubアカウントを間違えるという事故が無くなるはず。