Pulling to Keep up-to-date with Changes
When working on a team project, it’s important to ensure everyone stays up to date.
Before starting work on a project, you should update your local copy with the latest changes.
With Git, you can do this using the pull command.
pull combines two commands:
Let’s dive deeper into how fetch, merge, and pull work.
Git Fetch
fetch retrieves the entire change history of a tracked branch or repository.
In your local Git, use fetch to update and see what has changed on GitHub.
Example
[user@localhost] $
|
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), 733 bytes | 3.00 KiB/s, done. From https://github.com/code7school-test/hello-world e0b6038..d29d69f master -> origin/master
|
Now that we’ve retrieved the latest changes, we can check our status:
[user@localhost] $
|
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 origin/master by one commit. This is likely due to the updated README.md, but let’s double-check by viewing the log.
Example
[user@localhost] $
|
git log origin/master commit d29d69ffe2ee9e6df6fa0d313bb0592b50f3b853 (origin/master) Author: code7schools-test <[email protected]> Date: Fri Mar 26 14:59:14 2021 +0100
Updated README.md with a line about GitHub
commit e0b6038b1345e50aca8885d8fd322fc0e5765c3b (HEAD -> master) Merge: dfa79db 1f1584e Author: code7school-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 it by displaying the differences between our local master and origin/master.
Example
[user@localhost] $ |
git diff origin/master diff –git a/README.md b/README.md index 23a0122..a980c39 100644 — a/README.md +++ b/README.md @@ -2,6 +2,4 @@ Hello World repository for Git tutorial This is an example repository for the Git tutoial on https://www.code7school.com
-This repository is built step by step in the tutorial. – -It now includes steps for GitHub +This repository is built step by step in the tutorial. \ No newline at end of file
|
That looks exactly as expected! Now we can proceed with the merge.
Git Merge
Merging combines the current branch with a specified branch.
Since we’ve confirmed the updates are as expected, we can merge our current branch (master) with origin/master
.
Example
[user@localhost] $
|
git merge origin/master Updating e0b6038..d29d69f Fast-forward README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
|
Check our status again to ensure we are up to date.
Example
[user@localhost] $
|
git status On branch master Your branch is up to date with ‘origin/master’.
nothing to commit, working tree clean
|
There you go! Your local Git repository is up to date!
Git Pull
What if you just want to update your local repository without all those steps?
- The pull command combines fetch and merge to bring all changes from a remote repository into the current branch you’re working on.
- Now, make another change to the Readme.md file on GitHub.
- Use the
pull
command to update our local Git repository.
Example
[user@localhost] $
|
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/code7school-test/hello-world a7cdd4b..ab6b4ed master -> origin/master Updating a7cdd4b..ab6b4ed Fast-forward README.md | 2 ++ 1 file changed, 2 insertions(+)
|
That’s how you keep your local Git repository up to date with changes from a remote repository. In the next chapter, we’ll take a closer look at how the push command works on GitHub.