It's raining cats and dogs.

無駄なことなんてないはず

git clone で error: The requested URL returned error: 401 Authorization Required while accessing と怒られた

今新しく環境を作っていて、githubからとあるプロジェクトをcloneしようとしたら、表題の件で怒られた。

$ git clone https://github.com/sampleProject/hoge.git
Initialized empty Git repository in /path/to/hoge/.git/
error: The requested URL returned error: 401 Authorization Required while accessing https://github.com/sampleProject/hoge.git/info/refs

URLはダミーです

うちはセキュリティ上の制限で外に対してsshは開けていないのでhttpsでアクセスした。

調べてみると

https://help.github.com/articles/https-cloning-errors#check-your-git-version

が見つかる。
1.7.10以上必要です。なので、gitのバージョンを調べてみなさいと。

$ git --version
git version 1.7.1

バージョンが古いことはわかった。
が、この環境はインフラチームが用意してくれた環境で、OSはScientific Linux release 6.4、gitのインストールはyumを使っている。SL6.4のディストリビューションが提供しているyumのgitのバージョンは1.7.1のようだ。
パッケージ管理の都合上yumを使う必要があるのと、追加でyumリポジトリを追加するのはちょいと面倒。(ゴネればなんとかなるかもしれないが、多忙な彼らに頼むのは忍びない)

というわけでインフラチームに頼らずなんとかcloneしたかったのでいろいろ調べてみると

http://stackoverflow.com/questions/12538130/cant-clone-a-github-repo-on-linux-via-https

この辺りが見つかる。ビンゴ。

githubのURLを指定するときにgithubのusernameをURLに含めれば良さそう。

$ git clone https://<githubのユーザーID>@github.com/sampleProject/hoge.git
Initialized empty Git repository in /path/to/hoge/.git/
Password:
remote: Counting objects: 51114, done.
remote: Compressing objects: 100% (11818/11818), done.
remote: Total 51114 (delta 34851), reused 51036 (delta 34775)
Receiving objects: 100% (51114/51114), 37.25 MiB | 1.99 MiB/s, done.
Resolving deltas: 100% (34851/34851), done.

途中でパスワードを求められるので、入力するとcloneができた。
stackoverflowにも書いてあるけど

https://username:password@github.com/sampleProject/hoge.git

でパスワードをURLに含めてもいけそうだけど、historyに残ったりなんやらかんやらして嫌な感じなのでパスワードはURLに含めないようにした。

ちなみに、手元のmacだと、gitのバージョンは1.8.1.3だったので当然問題なくcloneできた。

なぜ1.7.10より古いgitだとうまくいくかはよくわかっていない。