MacからGitHubにSSH接続してpushする
やり方などメモ。
公開鍵・秘密鍵を作成登録する
cd ~/.sshssh-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 testcd test
Gitで管理できるようコマンドを打ち、プッシュ先のリモートリポジトリを指定する。
git init
## プッシュ先のリモートリポジトリを指定git remote add origin https://github.com/[account name]/test.git
testディレクトリ内にあるpushするファイルを指定
## 以下はtest.txtをpushする例git add test.txtgit 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/"