【git】SSH認証キーをGitLabに登録してclone

# 0. はじめに
大阪の受託開発エンジニアこと、kazumakishimoto([@kazuma_dev](https://twitter.com/kazuma_dev))です！
SSH認証キーをGitLabに登録して、リモートリポジトリからクローンする方法についてです！

## 0-1. 全体の流れ
> [1.SSH](#1-ssh)
> [2.clone](#2-clone)
> [まとめ](まとめ)
> [Reference](#reference)

## 0-2. 本記事の対象者
- SSH認証キーをGitLabに登録したい
- SSH接続でリモートリポジトリをクローンしたい

## 0-3. 前提条件
- GitLabアカウント作成済み
- GitLabでプロジェクト作成済み(本記事では『test』)
- 隠しファイル表示設定済み

https://gitlab.com/

https://qiita.com/TsukasaHasegawa/items/fa8e783a556dc1a08f51

## 0-4.要件
- GitLab登録
- SSH秘密鍵 / SSH公開鍵 / OpenSSHキー
- git clone

# 1.SSH
## 1-1.キーペア作成(秘密鍵と公開鍵)
```terminal:local
$ cd ~/.ssh

$ ssh-keygen -t rsa -f gitlab_rsa -C hoge@gmail.com
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase): #お好みのパスワードを入力
Enter same passphrase again: #再度お好みのパスワードを入力
Your identification has been saved in gitlab_rsa
Your public key has been saved in gitlab_rsa.pub
The key fingerprint is:
SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx hoge@gmail.com

$ ls ~/.ssh
gitlab_rsa. gitlab_rsa.pub
```

## 1-2.GitLabに公開鍵を登録
```terminal:local
$ pbcopy < ~/.ssh/gitlab_rsa.pub
```

https://gitlab.com/-/profile/keys

1. GitLabの`SSH Keys`にアクセス
1. `SSH Fingerprints`に`pbcopy`でコピーした`gitlab_rsa`をペースト
1. Titleは任意(gitlab_rsaなど)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/687994/f71fed28-79eb-0da2-9f26-03cc000e2b88.png)

## 1-3.ssh-agentに秘密鍵を登録
```terminal:local
$ ssh-add ~/.ssh/gitlab_rsa
Identity added: /Users/xxxxxxxxxxxxxxx/.ssh/gitlab_rsa (xxxxxxxxxxxxxxxxxxx@gmail.com)

$ ssh-add -l
xxxx SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxx@gmail.com (RSA)
```

## 1-4.configに反映
```terminal:local
$ vim ~/.ssh/config
```
```terminal:
# GitHub
Host github.com
  IdentityFile ~/.ssh/github_rsa
  User git

# GitLab
Host gitlab.com
  HostName gitlab.com
  User git
  AddKeysToAgent yes
  UseKeychain yes
  TCPKeepAlive yes
  identitiesonly yes
  IdentityFile ~/.ssh/gitlab_rsa
```

## 1-5.SSH接続
```terminal:local
$ ssh gitlab.com
The authenticity of host 'gitlab.com (xxxx:xxxx:xx:x:xxxx:xxxx:xxxx:xxxx)' can't be established.
ED25519 key fingerprint is SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes #yesを入力
Warning: Permanently added 'gitlab.com' (ED25519) to the list of known hosts.
Enter passphrase for key '/Users/kishimotokazuma/.ssh/gitlab_rsa': #パスワード入力
PTY allocation request failed on channel 0
Welcome to GitLab, @kazumakishimoto!
Connection to gitlab.com closed.
```

# 2.clone
## 2-1.git clone
```terminal:local
$ cd /Users/kishimotokazuma/Documents

$ git clone git@gitlab.com:kazumakishimoto/test.git
Cloning into 'test'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.

$ ls
test
```

# Reference
https://qiita.com/redamoon/items/07e445d1fce360cb5fa3

https://qiita.com/ucan-lab/items/21d62f1beaa8007c4ca9

https://zenn.dev/shotakaha/articles/ba3e97cfe0837721c3f3

https://laraweb.net/environment/8473/