LoginSignup
212
146

More than 5 years have passed since last update.

githubでユーザー名とパスワードを毎回聞かれる問題解消

Last updated at Posted at 2017-07-17

gitで毎回ユーザー名(アカウント名)とパスワードを聞かれてしまう

gitの鍵認証も済ませたのにも関わらず、pushするにしてもpullするにしても毎回gitからユーザー名(アカウント名)パスワードを聞かれてしまう。

[root@www ◯◯]# git pull
Username for 'https://github.com': △△××(ユーザー名)
Password for 'https://△△××@github.com': 

githubのユーザー名(△△××)とパスワードを毎回入力しないとpullやpushができない状態

原因と解決策

http通信になっているため。これをsshプロトコル通信に変えることで解決

現在のgitの通信確認

[root@www ◯◯]# git remote -v
origin  https://github.com/△△××/◯◯△△.git (fetch)
origin  https://github.com/△△××/◯◯△△.git (push)
[root@www ◯◯]#

上記だと、gitに毎回ユーザー名とパスワードを聞かれてしまう。
https://から始まっていることがダメ

※△△××はgithubのユーザー名
※◯◯△△はgithubのリモートのレポジトリ名

config --list でも確認できる。

[root@www ◯◯]# git config --list
user.name=△△××
user.email=△△××@gmail.com
remote.origin.url=https://github.com/△△××/◯◯△△.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*

user.name等がしっかり設定していても、remote.origin.urlhttps://から始まっているとダメ。

gitに毎回ユーザー名とパスワードを聞かれないようにする(解決への流れとコマンド)

1.まずは通信をsshに変える

[root@www ◯◯]# git remote set-url origin git@github.com:
[root@www ◯◯]# 
[root@www ◯◯]# git remote -v
origin  git@github.com: (fetch)
origin  git@github.com: (push)

これでhttp通信でなくなったのが分かる。
https://ではなくgit@になったことが重要。

2.リモートレポジトリをセットしてあげる

※ここは一番初めに確認したoriginのユーザー名から後ろをそのまま持って来れば良い。

[root@www ◯◯]# git remote set-url origin git@github.com:△△××/◯◯△△.git
[root@www ◯◯]# 
[root@www ◯◯]# git remote -v
origin  git@github.com:△△××/◯◯△△.git (fetch)
origin  git@github.com:△△××/◯◯△△.git (push)

これでssh通信で、ユーザー名と対象のレポジトリも含まれているのが分かる。

config --list でも確認できる。

[root@www ◯◯]# git config --list
user.name=△△××
user.email=△△××@gmail.com
remote.origin.url=git@github.com:△△××/◯◯△△.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*

git pull で確認してみよう

[root@www ◯◯]# git pull
Already up-to-date.

ユーザー名もパスワードも聞かれなくなった。

212
146
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
212
146