okochangの馬鹿でありがとう

ふらふら適当に世間を生きる日々でございます

githubへの初回pushでsshエラー

gitをテスト環境に入れてからgithubにpushしようとしたら、少しつまずいたのでメモ。
githubの始め方はこちらのブログで分かりやすく説明されているので割愛します。
gitのインストールとgithubリポジトリ(project_hoge)の作成は事前に完了しておいて下さい。

    • githubへのssh接続に使う秘密鍵と公開鍵のペアを作成
    • ※名前は自分で指定してますが、それがすべての始まり※公開鍵はgithubに追加してくださいね
$ ssh-keygen -t rsa -C "github-hoge"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/hoge/.ssh/id_rsa): /home/hoge/.ssh/hoge_rsa
Enter passphrase (empty for no passphrase):パスフレーズ
Enter same passphrase again:パスフレーズ
Your identification has been saved in /home/hoge/.ssh/hoge_rsa.
Your public key has been saved in /home/hoge/.ssh/hoge_rsa.pub.
The key fingerprint is:
    • gitの初期設定を行います
$ git config --global user.name "hoge"
$ git config --global user.email "hoge@example.com"
$ git config --global color.ui auto
    • git設定の確認
$ cat -n .gitconfig
     1     [user]
     2          name = hoge
     3          email = hoge@example.com
     4     [color]
     5          ui = auto

$ mkdir project_hoge
$ cd project_hoge/
$ git init
Initialized empty Git repository in /home/hoge/project_hoge/.git/
$ git var GIT_COMMITTER_IDENT
hoge  1292429310 +0900
$ git var GIT_AUTHOR_IDENT
hoge  1292429366 +0900
    • README作成、最初のコミット
$ touch README
$ git add README
$ git commit -m 'first commit'
[master (root-commit) 92e6b51] first commit
 1 files changed, 256 insertions(+), 0 deletions(-)
 create mode 100644 README
$ git remote add origin git@github.com:hoge/project_hoge.git
    • pushしようとするとエラーが発生!
$ git push origin master
Permission denied (publickey).
fatal: The remote end hung up unexpectedly
      SSHの鍵をデフォルトの名前にしなかった事があやしいです。

ここらへん

    を見るとそれらしき事が書いてます。
    • .ssh/configで使用する鍵を指定します
$ vi /home/hoge/.ssh/config
      1 Host                 github.com
      2 User                 git
      3 Port                 22
      4 Hostname             github.com
      5 IdentityFile         ~/.ssh/hoge_rsa
      6 TCPKeepAlive         yes
      7 IdentitiesOnly       yes

$ chmod 600 .ssh/config 
    • 再トライをすると無事に接続が出来ます
$ git push origin master
Enter passphrase for key '/home/hoge/.ssh/hoge_rsa':
Counting objects: 3, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 4.57 KiB, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:hoge/project_hoge.git
 * [new branch]      master -> master

以上、メモでしたー。
今日はクラウド勉強会に参加していましたが、とても勉強になりました。
おそらく年内は最後かもですが、勉強会はとても刺激になるので来年も積極的に参加したいですね。