Git認証情報について

参考: Git - 認証情報の保存

認証方法は大まかに以下の2種類に大別される。

  • SSH プロトコル
    • 秘密鍵、公開鍵を用いた認証を行う
    • パスフレーズなしの鍵生成(パスワード入力をスキップ)が可能
  • HTTP プロトコル
    • 接続のたびにユーザー名/パスワードを用いた認証を行う
    • 都度入力が必要となる(大変 → 後述の Credential Helper の活用)

どちらが使われるかは、リモートリポジトリのURLによって決定する

# HTTPSの場合
origin https://github.com/user/repo...

# SSHの場合
origin git@github.com:user/repo...

Git Credential Helper

HTTPプロトコルにおけるユーザーの反復入力を削減するためにGitが実行する外部プログラム

Git 長期ストレージ(メモリ内: 短期 からディスク上: 長期)から認証情報を管理する

設定場所

  • Local: .git/config
  • Global: ~/.gitconfig

優先度

なんらかのcredential 設定が見つかった時点で、ユーザー名とパスワードの取得を試みる。 両方を取得すると、それ以降のヘルパーは試行されない

つまり、なんらかの問題でそのcredential.helperが問題のあるユーザー・パスワードを返却する場合、403エラー等で失敗し続けることになる。 なので、この場合は、優先度高めのLocal等の設定ファイルでヘルパーリストを空にリセットし、その後指定したいヘルパーを設定すると回避できる 参考: https://git-scm.com/docs/gitcredentials

If credential.helper is configured to the empty string, this resets the helper list to empty (so you may override a helper set by a lower-priority config file by configuring the empty-string helper, followed by whatever set of helpers you would like).

例: codecommitのcredential.helperをLocalに設定しているのに、なぜかmacのosxkeychainが評価され403となる 対応: 以下を実行

git config --local credential.helper ""
git config --local --add credential.helper '!aws codecommit credential-helper $@'

コマンド

  • 全てのcredential helper設定を出所付きで確認
git config --list --show-origin
  • 現在のローカル設定を確認
git config --local --list | grep credential