LoginSignup
75
56

More than 3 years have passed since last update.

git パスワードを毎回聞かれる問題をHTTPSでも解決

Last updated at Posted at 2019-11-29

GitHubにgit pushするときに毎回ユーザ名とパスワードが聞かれますね.

ググるとSSHを使うページが多いですが,GitHubの推奨はHTTPSです.
HTTPSでも聞かれなくする方法を紹介します.

結論

HTTPSでCloneしたリポジトリで,

git config credential.helper store

を実行して,ユーザ名,パスワードをキャッシュさせる.

これで,最初に一度ユーザ名,パスワードを入力すればそれ以降は聞かれなくなります.

ただ,これだと平文でパスワードが保存されるので,嫌な場合は後の項目を参照ください.

解説

基本的には,公式のページに全部書いてあります.

HTTPSのほうが「ファイアウォールやプロキシ下でも使いやすい」のもあって推奨していてます.
その下のTipsの欄に,「パスワードを覚えさせるにはcredential helperを使うといいよ」って書いてあります.

GitHubとしては,メモリにキャッシュとして記憶するオプション (git config credential.helper cache) を紹介しています.
デフォルトは15分でキャッシュが切れます.

git config credential.helper storeとすると,ファイル (~/.git-credentials) にhttps://myusername:mypassword@github.comのような形で,平文で保存されます.

平文テキストは怖いんだが...

GitHubの解説ページによると,Macではosxkeychain helper,Windowsではwincredが使えます.また,設定すればLinux (Ubuntu) ではgnome-keyringなどが使えます.

以下,MacとWindowsは動作未検証です.

Macの場合

(1) osxkeychain helperがインストールされているか確認

$ git credential-osxkeychain
> Usage: git credential-osxkeychain <get|store|erase>

↑インストールされていなければ,requesting installと言ってインストールのプロンプトが出ます.

(2) osxkeychain helperをGitで使うように設定

$ git config --global credential.helper osxkeychain

Windowsの場合

下記だけでwincredというhelperを使うように設定できます.

git config --global credential.helper wincred

Linuxの場合

このページ を参考に設定します.

(1) gnome-keyringのmakeに必要なライブラリを入れる.

sudo apt install libgnome-keyring-dev

(2) makeする

cd /usr/share/doc/git/contrib/credential/gnome-keyring
sudo make

(3) gitにcredential-helperを設定

git config --global credential.helper /usr/share/doc/git/contrib/credential/gnome-keyring/git-credential-gnome-keyring

まとめ

credential.helperを設定しておけば,快適にGitが使えますね.
(ただ,当然ですがcredentialの情報が変わったときに再設定が必要なのでご注意.)

75
56
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
75
56