LoginSignup
12
18

More than 5 years have passed since last update.

GitHub への push で毎回ユーザー名とパスワードを聞かれるのはもうコリゴリなのです

Last updated at Posted at 2017-04-15

背景

タイトルのとおり、GitHub への push の際に
ユーザー名とパスワードを効かれるのがダルくなったんので書き残しておくことにした
※ 2段階認証を設定してからパスワード周りが面倒くさいってのもあった

原因

origin で指しているリモートリポジトリが http で通信を行うため

以下のように https://github.com/ryysud/golang-crud-api.git へ push するとユーザー名とパスワードが聞かれる

$ git remote -v
origin  https://github.com/ryysud/golang-crud-api.git (fetch)
origin  https://github.com/ryysud/golang-crud-api.git (push)

$ git push origin develop
Username for 'https://github.com':
Password for 'https://github.com':
remote: Anonymous access to ryysud/golang-crud-api.git denied.
fatal: Authentication failed for 'https://github.com/ryysud/golang-crud-api.git/'

解決方法

リモートリポジトリとの通信を ssh で行う

ssh で通信するように変更してあげればいいだけ

$ git remote set-url origin git@github.com:ryysud/golang-crud-api.git

これでも良いがリポジトリをクローンしてくる度に設定するのはとてもダルい
それを避けるためにも以下の設定を ~/.gitconfig に書くととても幸せになれる

[url "github.com:"]
    InsteadOf = https://github.com/
    InsteadOf = git@github.com:

いい感じ(๑•̀ㅂ•́)و✧

# ユーザー名とパスワードを聞かれる
$ git push origin develop
Username for 'https://github.com':
Password for 'https://github.com':
remote: Anonymous access to ryysud/golang-crud-api.git denied.
fatal: Authentication failed for 'https://github.com/ryysud/golang-crud-api.git/'

# https での通信を行うようになっている
$ git remote -v
origin  https://github.com/ryysud/golang-crud-api.git (fetch)
origin  https://github.com/ryysud/golang-crud-api.git (push)

# .gitconfig に URL の置換処理を追記 =======================
$ vi ~/.gitconfig
... 略
[url "github.com:"]
    InsteadOf = https://github.com/
    InsteadOf = git@github.com:
# =======================================================

# ssh での通信を行うように置換されていることがわかる
$ git remote -v
origin  github.com:ryysud/golang-crud-api.git (fetch)
origin  github.com:ryysud/golang-crud-api.git (push)

# ユーザー名とパスワードを聞かれることがなくなった
$ git push origin develop
Everything up-to-date

参考資料

12
18
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
12
18