Skip to content

Instantly share code, notes, and snippets.

@g-empr
Last active January 28, 2024 06:43
Show Gist options
  • Save g-empr/fc793caf3a0a18c31d8c708787bdf5f0 to your computer and use it in GitHub Desktop.
Save g-empr/fc793caf3a0a18c31d8c708787bdf5f0 to your computer and use it in GitHub Desktop.
GitHubでsshを使う.

ついでに複数のGitHubごとのマルチ設定も行う.

専用フォルダと鍵の作成

cd ~/.ssh
ssh-keygen -t rsa

質問はEnter*3. ​ ​

GitHubにkey登録

https://github.com/settings/ssh にアクセス.
SSH keys>New SSH keyをクリックし、Titleに適当な名称、keyに公開鍵を設定.
公開鍵は下記のように持ってくる.
Winの場合

clip < ~/.ssh/id_rsa.pub

Macの場合

pbcopy < ~/.ssh/id_rsa.pub

接続確認

接続確認してみる.

ssh -T git@github.com
Hi hogehoge! You've successfully authenticated, but GitHub does not provide shell access.

うまく接続できないとき

パスワードとか聞かれたらうまくいってないのでまず下記コマンドで確認.

git config remote.origin.url

確認したら以下のように設定.

git remote set-url origin git@github.com:[ユーザID]/[リポジトリ].git

~/.ssh/configの一行目に別のhostが指定してある場合は以下のように設定.

git remote set-url origin [Host名]:[ユーザID]/[リポジトリ].git

複数のsshを設定する

各鍵の作成

cd ~/.ssh/
ssh-keygen -t rsa -C '[対象メールアドレス]' -f ~/.ssh/id_rsa_[固有名] 

公開鍵のコピー

pbcopy < ~/.ssh/id_rsa_[固有名].pub

hostsの設定

下記に移動.
なければ作成する.

~/.ssh/config

下記内容を記述.

# 基本的な書き方
Host [ホスト名]
  HostName [各サービスのドメインまたはIPアドレス]
  User [ユーザ名: GitHubはgit、Bitbucketはアカウントのユーザ名(ユーザ名がhelloの場合hello)になる]
  Port 22
  IdentityFile [秘密鍵の場所]
  IdentitiesOnly yes

# 例 個人GitHubの場合
Host github.com
  HostName github.com
  User git
  Port 22
  IdentityFile ~/.ssh/id_rsa_github
  IdentitiesOnly yes

# 例 会社GitHubの場合
Host github.com.company
  HostName github.com
  User git
  Port 22
  IdentityFile ~/.ssh/id_rsa_github_company
  IdentitiesOnly yes

# 例 個人GitLabの場合
Host gitlab.com
  HostName gitlab.com
  User git
  Port 22
  IdentityFile ~/.ssh/id_rsa_gitlab_private
  IdentitiesOnly yes

# 例 会社GitLabの場合
Host git.company.co.jp
  HostName git.company.co.jp
  User git
  Port 2233
  IdentityFile ~/.ssh/id_rsa_gitlab_company
  IdentitiesOnly yes

ホストに接続確認

ssh -T [ホスト名]

# 例 個人GitHub
ssh -T github.com
# 例 会社GitHub
ssh -T github.com.company

clone などでアクセスするリポジトリの表記

git clone git@[ホスト名]:[サービスのユーザ名]/hogehoge.git

# 例 個人GitHub
git clone git@github.com:username/hogehoge.git
# 例 会社GitHub
git clone git@github.com.company:username/fugafuga.git
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment