12
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

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

Last updated at Posted at 2022-09-22

0. はじめに

大阪の受託開発エンジニアこと、kazumakishimoto(@kazuma_dev)です!
SSH認証キーをGitLabに登録して、リモートリポジトリからクローンする方法についてです!

0-1. 全体の流れ

1.SSH
2.clone
まとめ
Reference

0-2. 本記事の対象者

  • SSH認証キーをGitLabに登録したい
  • SSH接続でリモートリポジトリをクローンしたい

0-3. 前提条件

  • GitLabアカウント作成済み
  • GitLabでプロジェクト作成済み(本記事では『test』)
  • 隠しファイル表示設定済み

0-4.要件

  • GitLab登録
  • SSH秘密鍵 / SSH公開鍵 / OpenSSHキー
  • git clone

1.SSH

1-1.キーペア作成(秘密鍵と公開鍵)

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に公開鍵を登録

local
$ pbcopy < ~/.ssh/gitlab_rsa.pub

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

image.png

1-3.ssh-agentに秘密鍵を登録

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に反映

local
$ vim ~/.ssh/config
# 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接続

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

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

12
8
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
12
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?