TECH I.S.

GitLab からの Git プル


プラットフォームを変更します:

Shift focus to GitHubGitHubShift focus to BitbucketビットバケットShift focus to GitLabGitLab

変更内容について最新情報を得るためのプル

プロジェクトのチームで作業する際には、全員が最新情報について把握することが重要です。

プロジェクトに取り組む際には、常にローカルコピーに最新の変更を取得する必要があります。

Gitを使用すると、pullコマンドを使用して最新の変更を取得できます。

pullは2つの異なるコマンドの組み合わせです:

  • fetch
  • merge

fetchmergepullの動作について詳しく見てみましょう。


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

...
...

予想通りの結果ですが、ローカルのmasterorigin/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

しかし、これらの手順をすべて経ずに、ローカルリポジトリを更新したい場合はどうでしょうか?

pullfetchmerge を組み合わせたものです。これは、リモートリポジトリのすべての変更を作業中のブランチに取り込むために使用されます。

GitLab 上の Readme.md ファイルに別の変更を加えましょう。

GitLab Update Readme

ローカルの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の動作について詳しく説明します。


プログラミング学習を加速させる

プログラミングをプロの講師に教えてもらいませんか。

テックアイエスのプログラミングスクールは初心者も大歓迎です。年齢制限もありません。転職・副業に強く、挫折させない手厚いサポートで稼ぐ力を身につけましょう!

スクールの詳細