Ruby on Rails チュートリアル「第1章 ゼロからデプロイまで」の環境構築メモ(2) 〜SSHの公開鍵を作成してGitHubにpushするまで〜
GitHubリポジトリの作成
「New repository」を選択する。
「Repository name」を設定し、「Create repository」ボタンを押す。
秘密鍵と公開鍵の作成
~/.ssh
ディレクトリにid_rsa
とid_rsa.pub
の2つの鍵がなければ、鍵を作成する。
[vagrant@localhost first_app]$ ls ~/.ssh
鍵の作成には、ssh-keygen
を実行する。
- 鍵の保存先を聞かれるが、
/home/vagrant/.ssh/id_rsa
のままで問題なければ、Enter
を入力する。 - 次にpassphraseを2回聞かれるが、「鍵を使うときにパスフレーズを入力したくない場合は、パスフレーズを空のままに」しておく。
[vagrant@localhost first_app]$ ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/home/vagrant/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/vagrant/.ssh/id_rsa. Your public key has been saved in /home/vagrant/.ssh/id_rsa.pub. The key fingerprint is: (省略)
秘密鍵(id_rsa)と公開鍵(id_rsa.pub)が作成されていることを確認する。
[vagrant@localhost first_app]$ ls ~/.ssh id_rsa id_rsa.pub
SSH Keysの設定
「Settings」を選択し、profile画面で「SSH keys」を選択する。
「Add SSH key」を選択する。
先程作成しておいたid_rsa.pubの中身をコピーし、SSH Keys設定画面のKey欄に貼り付ける。
[vagrant@localhost first_app]$ vi ~/.ssh/id_rsa.pub
Title欄は任意のテキストを入力し、「Add key」を選択する。
RailsプロジェクトのファイルをGitHubに登録
システムセットアップ
Gitのコミットに必要なため、GitHubに登録済みのユーザー名とメールアドレスをそれぞれ登録する。
これらの情報は~/.gitconfig
ファイルに登録される。
[vagrant@localhost first_app]$ git config --global user.name "ユーザ名" [vagrant@localhost first_app]$ git config --global user.email メールアドレス [vagrant@localhost first_app]$ vi ~/.gitconfig [vagrant@localhost first_app]$ cat ~/.gitconfig [user] name = ユーザ名 email = メールアドレス
リポジトリの作成
git init
を実行すると、.git
ディレクトリが生成される。
[vagrant@localhost first_app]$ git init Initialized empty Git repository in /home/vagrant/rails_projects/first_app/.git/ [vagrant@localhost first_app]$ ls -all 合計 76 drwxrwxr-x 13 vagrant vagrant 4096 10月 20 08:11 2014 . drwxrwxr-x 3 vagrant vagrant 4096 10月 20 06:15 2014 .. drwxrwxr-x 7 vagrant vagrant 4096 10月 20 08:11 2014 .git -rw-rw-r-- 1 vagrant vagrant 466 10月 20 06:15 2014 .gitignore -rw-rw-r-- 1 vagrant vagrant 388 10月 20 06:16 2014 Gemfile (省略)
.gitignoreファイルの編集
Railsチュートリアルに従い、Gitの管理対象から除外するファイルを設定するため、.gitignore
ファイルを編集する。
[vagrant@localhost first_app]$ rm -rf .gitignore [vagrant@localhost first_app]$ vi .gitignore [vagrant@localhost first_app]$ cat .gitignore # Ignore bundler config. /.bundle # Ignore the default SQLite database. /db/*.sqlite3 /db/*.sqlite3-journal # Ignore all logfiles and tempfiles. /log/*.log /tmp # Ignore other unneeded files. doc/ *.swp *~ .project .DS_Store .idea .secret
インデックスへ登録
git add
は指定したファイルをインデックスに登録する。.
を指定することで、カレントディレクトリ内のすべてのファイル(サブディレクトリも含む)をインデックスに登録する。
[vagrant@localhost first_app]$ git add .
git status
はステージングエリアにあるファイルのリストを表示できる。
[vagrant@localhost first_app]$ git status # On branch master # # Initial commit # # Changes to be committed: # (use "git rm --cached <file>..." to unstage) # # new file: .gitignore # new file: Gemfile # new file: Gemfile.lock (省略)
インデックスに追加したファイルのコミット
git commit
する。-m
オプションをつけると、コミットメッセージを指定してコミットできる。
[vagrant@localhost first_app]$ git commit -m "Initialize repository" [master (root-commit) cfe92ba] Initialize repository 36 files changed, 804 insertions(+), 0 deletions(-) create mode 100644 .gitignore create mode 100644 Gemfile create mode 100644 Gemfile.lock (省略)
.git/configファイルの設定
今回はSSH Keysを設定しているので、.git/config
ファイルのremoteのプロトコルはSSHに変更する。
.git/config
ファイルのurl = https://github.com/ponkiti/first_app.git
という記述をurl = ssh://git@github.com:ponkiti/first_app.git
に変更する。
[vagrant@localhost first_app]$ vi .git/config [vagrant@localhost first_app]$ cat .git/config [core] repositoryformatversion = 0 filemode = true bare = false logallrefupdates = true [remote "origin"] url = ssh://git@github.com:ponkiti/first_app.git fetch = +refs/heads/*:refs/remotes/origin/*
リモートリポジトリへpush
ローカルレポジトリの内容をリモートリポジトリ(GitHub上に作成したリポジトリ)に反映させる。
git remote add origin git@github.com:ユーザー名/リポジトリ名.git
[vagrant@localhost first_app]$ git remote add origin git@github.com:ponkiti/first_app.git [vagrant@localhost first_app]$ git push -u origin master The authenticity of host 'github.com (192.30.252.131)' can't be established. RSA key fingerprint is XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'github.com,192.30.252.131' (RSA) to the list of known hosts. Counting objects: 60, done. Compressing objects: 100% (49/49), done. Writing objects: 100% (60/60), 13.86 KiB, done. Total 60 (delta 2), reused 0 (delta 0) To git@github.com:ponkiti/first_app.git * [new branch] master -> master Branch master set up to track remote branch master from origin.