Git Fetch Explained — Updating Your Local Repository from Remote

So far, we have been working as a single developer — creating branches locally and pushing them to GitHub.

But what happens when there are multiple developers?
Suppose you pushed your changes to GitHub, and another teammate also pushed new commits.
How will you bring their changes into your local repository?

👉 That’s exactly what the Git fetch command does.


Simulating the Scenario

  1. You have a repo cloned locally.
  2. A new file (github.txt) is added on GitHub by another developer.
  3. This creates a new commit on the remote repository.

On GitHub:


Commit: ba7902
File: github.txt

👉 Now GitHub’s origin/main is pointing to this new commit.
👉 But your local main branch is still behind.


Using Git Fetch

Run:

git fetch origin

👉 What happens:

  • Git downloads the new commit from GitHub.
  • Updates the remote tracking branches (origin/main, origin/HEAD).
  • But your local main branch remains unchanged.

Merging the Fetched Data

Now, to bring your local branch up to date, you need to merge:

git merge origin/main

👉 This fast-forwards your local main branch to match the remote.

✅ Now your local main branch and the remote main branch are pointing to the same latest commit.


Key Points About Git Fetch

  • Git fetch only downloads updates.
  • It does not merge automatically into your local branch.
  • Remote tracking branches (origin/main, origin/test) are updated.
  • You must merge manually (or rebase) to sync your local branch.

Difference from Git Pull

  • git fetch → Downloads data and updates remote tracking branches.
  • git pull → Downloads data and merges immediately into your current branch.

Summary

  • When teammates push changes to GitHub, your local repo doesn’t know until you run git fetch.
  • git fetch origin updates remote branch pointers (like origin/main).
  • Your local branches remain unchanged until you merge.
  • To update local main: git merge origin/main.
  • Fetch = safe way to see changes before merging.

Keep Learning 🚀

👉 Subscribe to Learning Ocean – Subscribers get coupon codes for my courses, early access to blogs and courses, and even exclusive YouTube videos.

👉 My YouTube Channel – More videos, more fun, and lots of learning!

👉 📺 Watch this topic in video form

Stay curious, keep coding, and let’s make learning fun together! 🎉