LoginSignup
242
132

More than 3 years have passed since last update.

githubを二段階認証に変更後に起こるエラーの対処方法

Last updated at Posted at 2016-08-07

はじめに

Githubの設定を二段階認証に変更をした。
二段階認証に変更するとgitの通信プロトコルがhttpsからgitに変わる仕様になっている。
そこで今まで使っていたリポジトリがhttps経由ではpush出来なくなるので、自分の遭遇した2つのエラー内容と解決方法をメモしておきます。

まず確認すること

まずssh keyが登録されているか ssh -T git@github.comのコマンドで確認。
正しく登録されていることが確認できた前提。

1.git pushに失敗する

gitpushをした際に変更前に聞かれなかったusernameとpasswordを求められ、正しいものを答えても認証に失敗する

Username for 'https://github.com': hoge-user
Password for 'https://hoge-user@github.com':
remote: Invalid username or password.
fatal: Authentication failed for 'https://github.com/hoge-user/repository-name.git/'

解決方法

該当するリポジトリまで移動し、Vimでvi .git/configを開きます。

[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
    ignorecase = true
    precomposeunicode = true
[remote "origin"]
    url = https://github.com:hoge/repository.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master

以上のようになっているかと思います。
[remote "origin"]内のurl = https://github.com:hoge/repository.giturl = git@github.com:hoge/repository.gitに変更します。
これでusernameとpasswordは聞かれずにpushすることができます。

2. git pushに失敗したもう一つのエラー

fatal: Unable to look up git@github.com:hoge (port 9418) (nodename nor servname provided, or not known)というエラーに遭遇した。

sshキーは正しく登録されているし、ドキュメントルートでless gitconfigを見てみてもおかしな記述がない。
何かと思い色々調べていて再度該当リポジトリでvi git/configを見てみると、

[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
    ignorecase = true
    precomposeunicode = true
[remote "origin"]
    url = git://git@github.com:hoge/repository.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master

という風に表示され、一見変な記述がないように見え、見逃していたのですが、url = git://git@github.com:hoge/repository.gitという記述になっていました。
この箇所をurl = git@github.com:hoge/repository.gitに修正することでpushがうまくいきました。

修正後

[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
    ignorecase = true
    precomposeunicode = true
[remote "origin"]
    url = git@github.com:hoge/repository.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master
242
132
7

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
242
132