MacからGitHubにSSH接続してpushする || note.sorakakeru.info

MacからGitHubにSSH接続してpushする

やり方などメモ。

公開鍵・秘密鍵を作成登録する

cd ~/.ssh
ssh-keygen -t rsa

## すでにid_rsaが存在する場合は上書きされるので別名で作成する
Enter file in which to save the key (/Users/(username)/.ssh/id_rsa):(id_rsaが存在する場合は任意の名前を指定)
Enter passphrase (empty for no passphrase):
Enter same passphrase again:

鍵ファイルの中身をコピーする

## 任意の名前ファイルを作成した場合はその名前のpubファイルを指定
pbcopy < ~/.ssh/id_rsa.pub

https://github.com/settings/sshにアクセスして鍵を登録する。
title: 任意の名前 Key: 上記コピーしたものをペースト

接続を確かめる

ssh -T git@github.com

## 柿が帰ってきたら完了
Hi [account name]! You've successfully authenticated, but GitHub does not provide shell access.

エラーが発生する場合(任意のファイル名で鍵を作成した場合など)

## ~/.ssh/ディレクトリ内にconfigファイルを作成し、以下の内容を保存
Host github github.com
  HostName github.com
  IdentityFile ~/.ssh/[鍵ファイル名(.pub不要)]
  User git

## 下記で接続確認
ssh -T github

GitHubにpushする

GitHubサイトからリモートリポジトリを作成する。
上部メニュー「New repository」> 「Repository Name」を指定して「Create repository」ボタンを押下。
(下記は「test」という名前で作成した場合)

リポジトリページの「Code」に書かれているurlをコピーする。

https://github.com/[account name]/test.git

ターミナルからGitで管理するディレクトリを作成し、移動する。

mkdir test
cd test

Gitで管理できるようコマンドを打ち、プッシュ先のリモートリポジトリを指定する。

git init

## プッシュ先のリモートリポジトリを指定
git remote add origin https://github.com/[account name]/test.git

testディレクトリ内にあるpushするファイルを指定

## 以下はtest.txtをpushする例
git add test.txt
git commit -m "Create test.txt"
git push origin master

GitHub上のリポジトリに追加されていたら成功。
git push origin masterを打った際にIDとPWを求められる場合は、接続がうまくいっていないので、Global設定に下記を追加する。

git config --global "url.git@github.com:.pushinsteadof" "https://github.com/"