LoginSignup
18
20

More than 5 years have passed since last update.

rails newしてからGitHubにリポジトリを作成するまでの流れ

Last updated at Posted at 2019-02-13

はじめに

ruby/railsの勉強を始めたら何かしらアプリを作成することになると思います。
railsチュートリアルを含め、rails newは何回か既にしていて、Bitbucketにレポジトリを作って・・・と作業をしてきましたが、作業をする都度手順を調べており効率が悪いため、自分で簡単に纏めようと思います。

想定する状況

これからRailsアプリを作成します。
GitHubにはアカウント登録済みです。
SSH keyも設定済みです。
(SSH作成の参考サイト⬇️
GitHubでssh接続する手順~公開鍵・秘密鍵の生成から~
Bitbucket Set up an SSH key
GitHub help

手順

rails newする

ローカルで新規のアプリを作成します。
データベースを指定する場合はrails new -d データベース名とします。
(その場合はデータベースを起動した状態でbin/rails db:createを実行。)
rails sで正常にブラウザ表示されるか確認。

GitHubでリポジトリを作成

Ripository nameは必須なので入力します。
Descriptionは任意で入力ください。
PublicにするかPrivateにするか選びましょう。
そのほかは未チェック(無視)で大丈夫です。

Gitコマンドを打つ

Railsアプリのルートディレクトリに移動してから以下コマンドを実行します。

git init   # 初期化
git add -A   # ステージング
git commit -m "コミットメッセージ"   # リポジトリに反映

GitHubにあるリモートリポジトリとローカルリポジトリを紐づける

git remote add origin https://github.com/xxxxxxx/[Ripository name].git
# SSHを用いてgitと通信するためには以下
git remote set-url origin git@github.com:xxxxxxx/[Ripository name].git
# 紐づけているリモートリポジトリを確認できる
git remote -v    

なお、いきなりgit remote set-url origin 〜をしてしまうと、fatal: No such remote 'origin'というエラーが出てしまいます。
先にremoteをaddする必要があり、set-urlはあくまでもremote先を変えるということになります。
SSHを始めから用いるのであれば、以下のコマンドとなります。

git remote add origin git@github.com:xxxxxxx/[Ripository name].git

ローカルリポジトリをリモートリポジトリにプッシュする

git push origin master
18
20
0

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
18
20