LoginSignup
0
0

More than 3 years have passed since last update.

Railsチュートリアル 第1章 - Gitによるバージョン管理

Posted at

リポジトリの作成とステージング

git initコマンドで、Gitリポジトリを新規作成します。

zsh
>>> pwd
~/docker/rails_tutorial_test/hello_app

>>> git init

git add -Aコマンドで、新規作成したGitリポジトリにプロジェクトのファイルを追加します。

zsh
>>> pwd
~/docker/rails_tutorial_test/hello_app

>>> git add -A

以上の操作は、この項目に至る以前の段階で既に行っていました。既にローカルのGitリポジトリは存在する状態です。

ステージングの状態確認とコミット

git statusコマンドで、ステージングの状態を表示することができます。

zsh
>>> 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オプションのあとに二重引用符付きでコミットメッセージを入力することにより、コミットメッセージも同時に設定することができます。

zsh
>>> git commit -m "Automatic changes"
...略

改めてgit statusを実行します。

zsh
>>> git status
On branch master
nothing to commit, working tree clean

「コミット対象となる変更はない」と表示されるようになりました。

なお、私自身としては、Git関係の操作について、Visual Studio Codeの機能で行うようにしています。

git logコマンドによるコミットメッセージの履歴の参照

git logコマンドで、コミットメッセージの履歴を参照できます。表示内容から、最新2件の履歴を抜粋してみました。

zsh
>>> 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にリモートリポジトリを構築する

zsh
>>> git remote add origin git@github.com:{GitHubのユーザ名}/hello_app.git

ローカルリポジトリに対応するリモートリポジトリを問題なく追加できた場合、git remoteは特にメッセージを返しません。

zsh
>>> 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コマンドを実行します。

zsh
>>> 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上での作業

zsh
>>> git checkout -b modify-README
Switched to a new branch 'modify-README'

>>> git branch
  master
* modify-README

Visual Studio Code上でも、トピックブランチがmodify-READMEとなっているのが見えます。

スクリーンショット 2019-07-29 13.25.41.png

README.mdの内容を変更してみましょう。

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コマンドを実行してみましょう。

zsh
>>>  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コマンドを使い、暗黙的なステージングから直接コミットしてみましょう。

zsh
>>> 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にマージする

zsh
>>> 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は削除しましょう。

zsh
>>> git branch -d modify-README
Deleted branch modify-README (was c26e68a).

メッセージを見るに、問題なくmodify-READMEブランチを削除できたようですね。

zsh
>>> git branch
* master

確かにブランチはmasterしかありません。

ローカルリポジトリの変更内容を、リモートリポジトリにgit pushする

この時点でのgit statusの実行内容です。

zsh
>>> 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コマンドにより、ローカルリポジトリの内容でリモートリポジトリを更新します。

zsh
>>> 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

正常にプッシュできたようです。

zsh
>>> 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'.となっていますね。

参考文献

0
0
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
0
0