LoginSignup
1
1

More than 3 years have passed since last update.

普段はGitHubだけどGitLab案件に放り込まれて上手くgit clone出来なかった人のために

Last updated at Posted at 2019-09-03

診断

  1. GitHubを使っている時は.ssh以下のConfigファイルにgithubのHostを書いていたか
  2. 鍵ファイルはデフォルトのid_rsaで作成して何も考えずに使っていた
  3. GitLabで新たに鍵ファイルを作成したか

以上3つのどれかに当てはまれば読んでください。

原因 - デフォルトで読みに行かないファイル名

ssh接続の際「~/.ssh/id_rsa」、「~/.ssh/id_dsa」、「~/.ssh/identity」しか
デフォルトでは見にいかない

新しく作成した鍵ファイルは上記の名前とは違うファイル名で作成しませんでしたか?
その場合は自動で読みに行ってはくれません。

参考
https://qiita.com/shizuma/items/2b2f873a0034839e47ce

対策 - ~/.ssh/configを作成しその中に

Host gitlab
  User {user}
  Port 22
  HostName {hostName}
  TCPKeepAlive yes
  identitiesonly yes
  identityFile {sshKey}

GitLabのgit cloneのコピペ部分にgit@gl.projects:account/app/test.gitとあるとします。
また鍵ファイルは~/.ssh/gitlab_id_rsaに作成したとします。

とすると上記の{}で囲った部分は下記のようになります。

Host gitlab
  User git
  Port 22
  HostName gl.projects
  TCPKeepAlive yes
  identitiesonly yes
  identityFile ~/.ssh/gitlab_id_rsa

参考資料
https://qiita.com/0084ken/items/2e4e9ae44ec5e01328f1

Host                   ホスト名
  HostName             ホストのアドレスかIPアドレス
  User                 ログインユーザ名
  IdentityFile         ログインするための秘密鍵のパス
  Port                 ポート番号(デフォルトは22)
  TCPKeepAlive         接続状態を継続したい場合:yes 継続しない場合:no
  IdentitiesOnly       IdentityFileが必要な場合:yes 必要ない場合:no
  ServerAliveInterval  一定期間サーバからデータが送られてこないときに、タイムアウトする秒数。(例) 120

テスト

下記の記述で接続できればGit Cloneも可能です!

ssh -T gitlab    // configのHostで指定した任意の名前

クローン

デフォルトの鍵ファイル名でやっていたときは何も考えずにGitHubやGitLabのコピペ場所の物をコピーしてきて
git clone + コピペ文字列
で出来ていましたが今回はconfigファイルを読みに行ってもらいます。

git clone gitlab:account/app/test.git

デフォルトの時に比べて:より前が変わっている事がわかりますね!

例)

# デフォルト
git clone git@gl.projects:account/app/test.git

# configを読む
git clone gitlab:account/app/test.git
1
1
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
1
1