変更内容について最新情報を得るためのプル
プロジェクトのチームで作業する際には、全員が最新情報について把握することが重要です。
プロジェクトに取り組む際には、常にローカルコピーに最新の変更を取得する必要があります。
Gitを使用すると、pull
コマンドを使用して最新の変更を取得できます。
pull
は2つの異なるコマンドの組み合わせです:
fetch
merge
fetch
、merge
、pull
の動作について詳しく見てみましょう。
Gitフェッチ
fetch
は、トラッキングされたブランチ/リポジトリの変更履歴を取得します。
したがって、ローカルのGitでは、fetch
を使用してGitLab上での変更を確認できます:
例
git fetch origin
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 2), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 374 bytes | 1024 bytes/s, done.
From https://gitlab.com/w3schools-test/hello-world
e0b6038..04023ee master -> origin/master
最新の変更
が取得できたので、status
を確認できます:
例
git status
On branch master
Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
(use "git pull" to update your local branch)
nothing to commit, working tree clean
私たちはorigin/master
から1つのcommit
遅れています。それは更新されたREADME.md
であるはずですが、log
を確認してダブルチェックしましょう:
例
git log origin/master
commit 04023eec575c3028ffa3e59d354ad1154cf199e0 (origin/master)
Author: w3schools test
Date: Wed Apr 28 07:16:03 2021 +0000
Updated README.md with a line about GitLab
commit e0b6038b1345e50aca8885d8fd322fc0e5765c3b (HEAD -> master)
Merge: dfa79db 1f1584e
Author: w3schools-test
Date: Fri Mar 26 12:42:56 2021 +0100
merged with hello-world-images after fixing conflicts
...
...
予想通りの結果ですが、ローカルのmaster
とorigin/master
の違いを表示して検証することもできます:
例
git diff origin/master
diff --git a/README.md b/README.md
index cd7cc92..a980c39 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,5 @@
# hello-world
Hello World repository for Git tutorial
This is an example repository for the Git tutoial on https://www.w3schools.com
-This tutoial focuses mainly on Git and using GitLab as its remote.
-This repository is built step by step in the tutorial.
+This repository is built step by step in the tutorial.
No newline at end of file
予想どおりの結果ですね!これで安全にmerge
を行うことができます。
Gitのマージ
merge
は、現在のブランチと指定したブランチを結合します。
更新内容が予想通りであることを確認し、現在のブランチ(master
)をorigin/master
とマージします:
例
git merge origin/master
Updating e0b6038..04023ee
Fast-forward
README.md | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
改めて status
を確認して、最新の状態であることを確認しましょう:
例
git status
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
これで、ローカルのGitが最新の状態になりました!
Git Pull
しかし、これらの手順をすべて経ずに、ローカルリポジトリを更新したい場合はどうでしょうか?
pull
は fetch
と merge
を組み合わせたものです。これは、リモートリポジトリのすべての変更を作業中のブランチに取り込むために使用されます。
GitLab 上の Readme.md ファイルに別の変更を加えましょう。
ローカルのGitを更新するために pull
を使用します:
例
git pull
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 2), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 357 bytes | 1024 bytes/s, done.
From https://gitlab.com/w3schools-test/hello-world
04023ee..d50b0ea master -> origin/master
Updating 04023ee..d50b0ea
Fast-forward
README.md | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
これは、リモートリポジトリからローカルのGitを最新の状態に保つ方法です。次の章では、GitLabでのpush
の動作について詳しく説明します。
プログラミング学習を加速させる
プログラミングをプロの講師に教えてもらいませんか。