Git Merge
Pulling to Keep up-to-date with Changes
When working as a team on a project, it is important that everyone can stay 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 actually a combination of 2 different commands:
fetch
merge
Lets take a closer look into how fetch
, merge
, and pull
works.
Git Fetch
fetch
gets all the change history of a tracked branch/repo.
Now we are going to create a README.md
file for our repository on GitHub. It is recommended that all repositories have a readme file, and that it describes the repository.
And commit
:
Head back to your local Git, and fetch
updates:
Example
git fetch origin
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 806 bytes | 4.00 KiB/s, done.
From https://github.com/w3schools-test/hello-world
9ab23f8..a7cdd4b main -> origin/main
Now that we have the recent changes
, we can check our
status
:
Example
git status
On branch main
Your branch is behind 'origin/main' 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/main
by 1
commit
. That should be the included
README.md
, but lets double check by viewing the
log
:
Example
git log origin/main
commit a7cdd4bf8f851b8de08d8b26be4ec82b371f4b48 (origin/main)
Author: w3schools-test <[email protected]>
Date: Thu Mar 25 11:41:24 2021 +0100
Created Readme.md
commit 9ab23f8e199880def2dfa775ae4868839d999747 (HEAD -> main)
Merge: 4068962 aad81e1
Author: w3schools-test <[email protected]>
Date: Tue Mar 23 15:15:21 2021 +0100
Merge pull request #2 from w3schools-test/new-style
New style looks good
...
...
That looks as expected, but we can also verify by showing the differences
between our local main
and
origin/main
:
Example
git diff main...origin/main
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..f43f0fc
--- /dev/null
+++ b/README.md
@@ -0,0 +1,3 @@
+# hello-world
+Hello World repository for Git tutorial
+This is an example repository for the Git tutoial on https://www.w3schools.com
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 (main
), with
origin/main
:
Example
git merge origin/main
Updating 9ab23f8..a7cdd4b
Fast-forward
README.md | 3 +++
1 file changed, 3 insertions(+)
create mode 100644 README.md
Lets check our status
again to confirm we are up to date:
Example
git status
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean
There! Your local git is up to date, and you can start working on your project!
Git Pull
But what if you just want to update your local repo, 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.
Lets make another change to the Readme.md file on GitHub.
Use pull
to update our local Git:
Example
git pull origin
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 794 bytes | 1024 bytes/s, done.
From https://github.com/w3schools-test/hello-world
a7cdd4b..ab6b4ed main -> origin/main
Updating a7cdd4b..ab6b4ed
Fast-forward
README.md | 2 ++
1 file changed, 2 insertions(+)
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
pull
and pull requests
work on GitHub.