リポジトリの作成とステージング
git init
コマンドで、Gitリポジトリを新規作成します。
>>> pwd
~/docker/rails_tutorial_test/hello_app
>>> git init
git add -A
コマンドで、新規作成したGitリポジトリにプロジェクトのファイルを追加します。
>>> pwd
~/docker/rails_tutorial_test/hello_app
>>> git add -A
以上の操作は、この項目に至る以前の段階で既に行っていました。既にローカルのGitリポジトリは存在する状態です。
ステージングの状態確認とコミット
git status
コマンドで、ステージングの状態を表示することができます。
>>> git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: Gemfile.lock
no changes added to commit (use "git add" and/or "git commit -a")
以上のメッセージが表示されました。というわけで、Gemfile.lock
をコミットしてみます。
ステージング済みの変更をコミットするためには、git commit
コマンドを使います。-m
オプションのあとに二重引用符付きでコミットメッセージを入力することにより、コミットメッセージも同時に設定することができます。
>>> git commit -m "Automatic changes"
...略
改めてgit status
を実行します。
>>> git status
On branch master
nothing to commit, working tree clean
「コミット対象となる変更はない」と表示されるようになりました。
なお、私自身としては、Git関係の操作について、Visual Studio Codeの機能で行うようにしています。
git log
コマンドによるコミットメッセージの履歴の参照
git log
コマンドで、コミットメッセージの履歴を参照できます。表示内容から、最新2件の履歴を抜粋してみました。
>>> git log
commit 676fb27c499bb8f326be7509a6617cd0884281ad (HEAD -> master)
Author: ...略
Date: Wed Jul 24 19:52:52 2019 +0900
Automatic changes
commit 0fd05fbc47f992b4005048eafaed5d87d28b62e7
Author: ...略
Date: Tue Jul 23 14:39:52 2019 +0900
Change 'application#hello' -> 'application#goodbye'
...略
git log
コマンドは、less
コマンドをインターフェースとして使用しています。1画面に収まりきらないログの場合は、q
キーを押せばgit log
を終了することができます。
ローカルリポジトリを元として、GitHubにリモートリポジトリを構築する
>>> git remote add origin git@github.com:{GitHubのユーザ名}/hello_app.git
ローカルリポジトリに対応するリモートリポジトリを問題なく追加できた場合、git remote
は特にメッセージを返しません。
>>> pwd
~/docker/rails_tutorial_test/hello_app
>>> git push -u origin --all
ERROR: Repository not found.
fatal: Could not read from remote repository.
あれ?git push
ができないですね。原因は何でしょう。
…先にWebブラウザでGitHubにアクセスし、リモートリポジトリの受け皿を作成しなければならないのだったのですね。というわけで、WebブラウザでGitHubにアクセスし、hello_app
という名前のリモートリポジトリを作成します。その上で改めてgit push
コマンドを実行します。
>>> git push -u origin --all
Enumerating objects: 118, done.
...略
Branch 'master' set up to track remote branch 'master' from 'origin'.
正常にgit push
できたようです。WebブラウザでGitHubのhello_app
リポジトリにアクセスしたときにも、ローカルと同じディレクトリツリーが見えます。
ブランチmodify-README
の作成と、modify-README
上での作業
>>> git checkout -b modify-README
Switched to a new branch 'modify-README'
>>> git branch
master
* modify-README
Visual Studio Code上でも、トピックブランチがmodify-README
となっているのが見えます。

README.md
の内容を変更してみましょう。
-# README
+# Ruby on Rails Tutorial
-This README would normally document whatever steps are necessary to get the
-application up and running.
+## "hello, world!"
-Things you may want to cover:
-
-* Ruby version
-
-* System dependencies
-
-* Configuration
-
-* Database creation
-
-* Database initialization
-
-* How to run the test suite
-
-* Services (job queues, cache servers, search engines, etc.)
-
-* Deployment instructions
-
-* ...
+This is the first application for the
+[*Ruby on Rails Tutorial*](https://railstutorial.jp/)
+by [Michael Hartl](http://www.michaelhartl.com/). Hello, world!
以上の変更内容を保存した時点で、git status
コマンドを実行してみましょう。
>>> git status
On branch modify-README
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: README.md
no changes added to commit (use "git add" and/or "git commit -a")
「README.md
が変更されたものの、まだステージングされていない」というメッセージが表示されました。ここから「git add -A
コマンドを使って明示的にステージング→git commit
コマンドを使ってコミット」という手順を実行してもいいのですが、今回はgit commit -a
コマンドを使い、暗黙的なステージングから直接コミットしてみましょう。
>>> git commit -a -m "Improve the README file"
[modify-README c6cc6ad] Improve the README file
1 file changed, 5 insertions(+), 22 deletions(-)
正常にコミットできたようです。
ブランチmodify-README
での作業内容をmaster
にマージする
>>> git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
>>> git merge modify-README
Updating 676fb27..c26e68a
Fast-forward
README.md | 27 +++++----------------------
1 file changed, 5 insertions(+), 22 deletions(-)
git checkout master
コマンドのところで、Railsチュートリアルに記載のないYour branch is up to date with 'origin/master'.
というメッセージが表示されています。これは、大雑把に言って「ローカルのmaster
ブランチとリモートのorigin/master
ブランチの進度は一致している」という意味のメッセージです。
メッセージを見るに、マージは正常に完了したようです。
ブランチmodify-README
の削除
ブランチmodify-README
の内容はmaster
にマージされましたので、不要になったブランチmodify-README
は削除しましょう。
>>> git branch -d modify-README
Deleted branch modify-README (was c26e68a).
メッセージを見るに、問題なくmodify-README
ブランチを削除できたようですね。
>>> git branch
* master
確かにブランチはmaster
しかありません。
ローカルリポジトリの変更内容を、リモートリポジトリにgit push
する
この時点でのgit status
の実行内容です。
>>> git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
先程Your branch is up to date with 'origin/master'.
であったところが、Your branch is ahead of 'origin/master' by 1 commit.
に変わっています。大雑把に言って、「ローカルのmaster
ブランチが、リモートのorigin/master
ブランチより1コミットだけ先を進んでいる」という意味です。この場合、git push
コマンドにより、ローカルリポジトリの内容でリモートリポジトリを更新します。
>>> git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
```:zsh
>>> git push
Enumerating objects: 5, done.
...略
676fb27..c26e68a master -> master
正常にプッシュできたようです。
>>> git status
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
Your branch is up to date with 'origin/master'.
となっていますね。