Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mookjp
Created March 24, 2014 12:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mookjp/9739294 to your computer and use it in GitHub Desktop.
Save mookjp/9739294 to your computer and use it in GitHub Desktop.

GitのHTTP認証が面倒なときの解決策

GitのリモートリポジトリへHTTP認証を使ってアクセスしていると、 毎回ユーザー名・パスワードを求められるので面倒です。 この入力を回避するには下記の方法があります。

1. 認証の設定を書く

gitconfigに下記の記述を加えます。

[credential "https://example.com"]
    username = me

Gitは設定したURLに対して、ホスト名とプロトコルがマッチしているかを見ています。 例として、上記の設定がgitconfigに記述されていた場合、マッチするURLは以下のようになります。

https://example.com/hoge.git // マッチする
http://example.com/hoge.git // マッチしない
https://hoge.example.com/hoge.git // マッチしない

2. Credential helpersを使う

GitのCredential helpersを使うことによって、GitもしくはGit以外のプログラムが 提供するパスワード管理システムを認証に利用することができます。

Credential helpersには以下の2種類があります。

  • cache
    • 一時的に認証を保持
    • メモリ上にパスワードを保持し、タイムアウト後は破棄
  • store
    • 無期限に認証を保持
    • ファイル(デフォルトでは~/.git-credentials)に平文でユーザー名とパスワードを保持

環境によってはサードパーティ製のhelperがあるようです。 利用できるhelperはgit help -a | grep credentialで確認できます。

$ git help -a | grep credential
    credential                relink
    credential-cache          remote
    credential-cache--daemon  remote-ext
    credential-store          remote-fd
    credential-osxkeychain  remote-hg

Macの場合はcachestoreの他にもosxkeychainがあることが確認できます。

osxkeychainを指定した場合は、Keychainがパスワードを保持します。保存されたパスワードはKeychain Accessから確認可能です。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment