mthiroshi_blog (ラーメン IT系 筋トレ)

普段の生活から仕事のIT系技術、趣味のラーメンなど書いていきます。

git config user.name と user.email を切り替えるshell script を書いたのでメモ

前提

仕事用と個人用のgithubを1台のPCで利用しているときに、githubにpushできないので切り替えたい時がある。 それ用にscript書いたので備忘録。

git config切り替えscript

git-config-switch

#!/bin/bash

git-config-switch-business() {
    git config --global user.name "hogehoge"
    git config --global user.email "hogehoge@hogehogehoge.hoge"
}

git-config-switch-private() {
    git config --global user.name "foobar"
    git config --global user.email "foobar@foobar.foobar"
}

main() {
    case "$1" in
    'business')
        git-config-switch-business
        echo '🍺 git config switched for business. 🍺'
        ;;
    'private')
        git-config-switch-private
        echo '🍺 git config switched for private. 🍺'
        ;;
    esac

    git config user.name
    git config user.email
}

main $@
exit 0

もっときれいな書き方できると思うけど、一旦はこれでいいかな。 コマンドの出力がわかりにくかったので絵文字つけてみた。

scritpを書いたパスを通す。

export PATH=$PATH:<scriptを置いたディレクトリ>

実行権限をつけてscirptを実行

~$ chmod +x git-config-switch
~$ soruch .bashrc
~$ git-config-switch business
🍺 git config switched for business. 🍺
hogehoge
hogehoge@hogehoge.hoge

下記の記事を参考にさせていただきました。

qiita.com

privateのリモートリポジトリを github.com-hogeho に書き換えて、 ssh/config にhostnameと鍵の設定を書いています。

余談

git switchってコマンドは既にあるみたい。checkout の実験的な新機能とのこと。 qiita.com