Git Pull from GitLab
Pulling to Keep up-to-date with Changes
When working as a team on a project, it is important that everyone stays up to date.
Any time you start working on a project, you should get the most recent changes to your local copy.
With Git, you can do that with pull
.
pull
is a combination of 2 different commands:
fetch
merge
Let's take a closer look into how fetch
, merge
, and pull
works.
Git Fetch
fetch
gets all the change history of a tracked branch/repo.
So, on your local Git, fetch
updates to see what has changed on GitLab:
Example
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
Now that we have the recent changes
, we can check our
status
:
Example
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
We are behind the origin/master
by 1
commit
. That should be the updated
README.md
, but lets double check by viewing the
log
:
Example
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
...
...
That looks as expected, but we can also verify by showing the differences
between our local master
and
origin/master
:
Example
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
That looks precisely as expected! Now we can safely merge
.
Git Merge
merge
combines the current branch, with a specified branch.
We have confirmed that the updates are as expected, and we can merge our current branch (master
) with
origin/master
:
Example
git merge origin/master
Updating e0b6038..04023ee
Fast-forward
README.md | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
Check our status
again to confirm we are up to date:
Example
git status
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
There! Your local git is up to date!
Git Pull
But what if you just want to update your local repository, without going through all those steps?
pull
is a combination of fetch
and merge
. It is used to pull all changes from
a remote repository into the branch you are working on.
Make another change to the Readme.md file on GitLab.
Use pull
to update our local Git:
Example
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(-)
That is how you keep your local Git up to date from a remote repository. In the next chapter, we will look closer at how
push
works on GitLab.