LoginSignup
20

More than 3 years have passed since last update.

初めてのgitは5ステップで完了

Last updated at Posted at 2019-08-25

はじめに

no such identity: /Users/ユーザ名/.ssh/id_rsaエラーに遭遇し、解決策を調べる中で学んだ、gitをはじめる際の5つのステップをまとめます。

ちなみに、上記エラーに対しての対処法は、Step4に記しています。

5つのステップ

Step1: 鍵ペアを作成

ssh-keygenコマンドで、公開鍵、秘密鍵のペアを作成する。

-tオプションで鍵の種類(ここでは、RSA)を指定し、-Cオプションではコメントを登録するが、メールアドレスを指定するのが一般的だそう。また、-bオプションで鍵の長さを指定するが、セキュリティの観点から4096を指定すると良い(デフォルトでは2048が多いよう)。

さらに、-fオプションでファイル名を指定することができる。鍵を作成する場面は、Githubと通信する時以外にもあり、例えば、AWSにアプリをデプロイするときや、VPSと接続する時など多々ある。その度、デフォルトでは、鍵は全て、id_rsaid_rsa.pubという名前で作られるため、適宜変更していく必要がある。
よって、Githubで利用する鍵の名前は、github_rsa(もしくは、github_id_rsa)のように変更するとする良い。

コマンド実行後、まずは、SSH Keysの保存先を聞かれるが、特に問題なければEnterキーを押す。次に、パスフレーズ(パスワードのようなもの)を入力する。ここで、入力したパスフレーズは画面に表示されないことに注意する。

❯ ssh-keygen -t rsa -b 4096 -C "takuyanin@gmail.com" -f github_rsa

Generating public/private rsa key pair.
Enter file in which to save the key (/Users/takuyanin/.ssh/github_rsa): (Enterを押す)
Enter passphrase (empty for no passphrase): (パスワードを入力)
Enter same passphrase again: (再度パスワードを入力)
Your identification has been saved in /Users/takuyanin/.ssh/github_rsa.
Your public key has been saved in /Users/takuyanin/.ssh/github_rsa.pub.
The key fingerprint is:
SHA256:UpcWqlhLGY1RN2iAQWfBDRysjAja0CGAz2rbcUiCpoY takuyanin@gmail.com
The key's randomart image is:
+---[RSA 4096]----+
|=..oo*X@..+      |
|+.. .o=o=o +     |
|+* o .+.o +      |
|+o=.o+ + o       |
|+.o o + S        |
|Eo o . .         |
|o o o            |
| . .             |
|                 |
+----[SHA256]-----+

実行後、github_rsa(秘密鍵)と、github_rsa.pub(公開鍵)が~/.sshフォルダに作成される。
また、key fingerprintも表示される。key fingerprintとは言葉通り、まさに鍵の指紋のようなもので、データが改ざんされていないか確認するために使われるよう。より詳細に知りたい方は、ここをチェック。

Step2: 秘密鍵の登録(任意)

ここから、~/.sshフォルダでコマンドを実行する(別の場所からでも問題ないが、タイプが長くなる)。

下記のコマンドを実行すると、秘密鍵をssh-agentデーモンに登録することで、SSH接続の際にパスワードの入力を省くことができる。

❯ ssh-add -K github_id_rsa

Identity added: /Users/takuyanin/.ssh/github_id_rsa (takuyanin@gmail.com)

次のコマンドでssh-addがきちんと行われたかを確認できる。

❯ ssh-add -l

4096
SHA256:UpcWqlhLGY1RN2iAQWfBDRysjAja0CGAz2rbcUiCpoY takuyanin@gmail.com

Step3: 公開鍵をGithubに配置

まずは、pbcopyコマンドで公開鍵の中身をクリップボードにコピーする。

❯ pbcopy < github_id_rsa.pub

そして、GithubのSettingsを開き、Githubに鍵登録する。

1. 「New SSH key」をクリック

SSH_and_GPG_keys.png

2. 公開鍵をペースト

Add_new_SSH_keys.png

そして、「Add SSH key」をクリックして鍵の登録が完了。

Step4: SSH接続できるか確認

❯ ssh -T git@github.com

no such identity: /Users/takuyanin/.ssh/id_rsa: No such file or directory
git@github.com: Permission denied (publickey).

上記コマンドを実行した時に、no such identity: /Users/takuyanin/.ssh/id_rsa: No such file or directory
git@github.com: Permission denied (publickey).
といったエラーが出た場合は、configファイル(がなければ作成し、)に下記のように設定する。
もしかしたら、IdentityFile~/.ssh/id_rsaのままかも(名前を変更したため、ここも変更する必要がある)。

また、.ssh/configとは、SSH経由でリモートサーバと接続する際に利用される設定ファイル。

~/.ssh/config
Host github
    HostName github.com
    IdentityFile ~/.ssh/github_id_rsa
    User git

もう一度実行し、次のように出力されれば、成功。

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

Step5: プロジェクト始動

5.1: Githubでレポジトリ作成

まずは、プロジェクト(hoge)をGithubで作成する。「New repository」から、プロジェクト名を記入し、レポジトリを作成する。

Create_a_New_Repository.png

5.2: ローカルでプロジェクトを作成

作成後は、そのプロジェクトディレクトリに移動して作業する。

mkdir /Users/takuyanin/hoge
❯ cd /Users/takuyanin/hoge

5.3: 初期化

❯ git init 

Initialized empty Git repository in /Users/takuyanin/hoge/.git/

5.4: ファイル作成

touch README.md
❯ echo "# hoge" >> README.md

5.5: ステージングする

❯ git add .

5.6: コミットする

❯ git commit -m "first commit"

5.7: リモートリポジトリと連携

最初にGithub上で作った、リモートリポジトリとローカルのリポジトリを連携する。

次の2つのどちらかを実行する

1. git@で登録

❯ git remote add origin git@github.com:takuyanin/hoge.git

2. httpsで登録

❯ git remote add origin https://github.com/takuyanin/hoge

そして、きちんと連携されたか確認。

❯ git remote -v

origin  git@github.com:takuyanin/hoge.git (fetch)
origin  git@github.com:takuyanin/hoge.git (push)

5.8: ローカルでの変更をリモートと同期

origin(Githubとしてもリモートリポジトリ)のmaster(メインブランチ)にプッシュする。

❯ git push origin master

補足

ステップ1で作成した公開鍵のフィンガープリント(鍵の指紋)を確認したい場合は、ssh-keygenコマンドで確認できる。

❯ ssh-keygen -lf ~/.ssh/github_id_rsa.pub

4096 SHA256:UpcWqlhLGY1RN2iAQWfBDRysjAja0CGAz2rbcUiCpoY takuyanin@gmail.com (RSA)

参考

まとめ

上記に記しましたが、初めてGithubを利用する際は、次のようなステップをふむ必要があります。

Step1: 鍵ペアを作成
Step2: 秘密鍵の登録(任意)
Step3: 公開鍵をGithubに配置
Step4: SSH接続できるか確認
Step5: プロジェクト始動

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
20