現象
gitlabへのpush時に以下のエラーが出てpushできない。
gitlabのバージョン:6-3-stable
$ git push -u origin master
fatal: 'username/test01.git' does not appear to be a git repository
fatal: The remote end hung up unexpectedly
この時のoriginは以下のとおり。
git@gitlab.cadence:username/test01.git
以下のようにすればpushできるが、変なのでrepositoriesなしでもpushできるようにしたい。
git@gitlab.cadence:repositories/username/test01.git
原因
以下のサイトによればgitlabの設定前に公開鍵が~/.ssh/authorized_keysに設定されていると発生する。
確かにAnsibleでGiltabを構築するでは公開鍵を登録していた。。
gitlab public wiki - Could not read from remote repository
解決策
gitlab用の公開鍵・秘密鍵ペアを作成する。
- 公開鍵・秘密鍵を作成
$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/xxx/.ssh/id_rsa): /Users/xxx/.ssh/id_rsa_gitlab
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/xxx/.ssh/id_rsa_gitlab.
Your public key has been saved in /Users/xxx/.ssh/id_rsa_gitlab.pub.
The key fingerprint is:
xxxx
The key's randomart image is:
xxxx
gitlabへの公開鍵の登録
Profile settings > SSH Keys > Add SSH Keysで登録する。~/.ssh/configの編集
鍵を指定してアクセスするように~/.ssh/configを編集する。
~/.ssh/config
Host gitlab.cadence
User git
Port 22
Hostname gitlab.cadence
IdentityFile ~/.ssh/id_rsa_gitlab
- git設定ファイルの編集 リポジトリ毎などの設定に合わせて編集する。
[remote "origin"]
url = git@gitlab.cadence:username/test01.git
fetch = +refs/heads/*:refs/remotes/origin/*
もしくは以下のコマンドでOK(というかこっちの方がわかりやすい)
$ git remote add origin git@gitlab.cadence:username/test01.git
以上