3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【mac】github複数アカウントをdirenvを使って環境変数で切り替える

Last updated at Posted at 2021-12-23

仕事とプライベートでgithubアカウントを分けて運用しています。

.gitconfigで設定を分けていたのですが、direnvが便利だそうなので設定してみます。

direnv is 何?

現在いるディレクトリに環境変数をロード/アンロードする拡張機能をシェルに追加できます。

direnvのインストール

$ brew install direnv

インストール後はバージョンを確認

$ direnv --version

公式のとおり.zshrcに追記

eval "$(direnv hook zsh)"

.zshrc再読み込み

$ source ~/.bashrc

gitの環境変数を設定

下記のように仕事用ディレクトリとプライベート用ディレクトリがある場合、それぞれのディレクトリ直下に.envrcファイルを設置する。

├ work/
└ private/

.envrcに環境変数1を追記

export GIT_COMMITTER_NAME=ユーザー名
export GIT_COMMITTER_EMAIL=メールアドレス
export GIT_AUTHOR_NAME=ユーザー名
export GIT_AUTHOR_EMAIL=メールアドレス
export GIT_SSH_COMMAND="ssh -i /user/to/path/sshkeyファイル"

.envrcの内容を反映する

$ direnv allow

これでgit commit、git push時に環境変数を参照するようになります。
今回の設定(環境変数の設定)だとgit config --listではuser.nameやuser.emailは表示されないので、git commit後にgit logを念の為確認しましょう。

$ git log
  1. ちなみに環境変数はGit公式ドキュメントに記載があります。GIT_SSH_COMMANDGIT_AUTHOR_NAME,GIT_AUTHOR_EMAILほか

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?