Lesson 35 of 60 – Git Fetch
58%

Git Fetch

The git fetch command is used to download information about changes from a remote repository without directly integrating those changes into your current working branch.

Note: git fetch is useful when you want to see what changed on the remote repository before deciding whether and how to integrate those changes.

1. What is Git Fetch?

git fetch downloads references and objects from a remote repository and updates the corresponding remote-tracking branches.

Remote Repository
       ↓
   git fetch
       ↓
Local Repository
       ↓
Remote-tracking Branches

2. Why Use Git Fetch?

Git fetch is useful when you want to inspect remote changes before integrating them into your current branch.

  • Check remote updates
  • Review new commits
  • Compare local and remote branches
  • Update remote-tracking branches
  • Avoid immediately changing your working branch

3. Basic Git Fetch Syntax

The basic syntax is:

git fetch <remote>

For example:

git fetch origin

This fetches updates from the remote named origin.

4. Fetch from Origin

origin is commonly the name assigned to the main remote repository.

git fetch origin

Git retrieves information from that remote and updates its remote-tracking references.

5. Checking Remote Repositories

Use the following command to view configured remotes:

git remote -v

Example:

origin  https://github.com/username/project.git (fetch)
origin  https://github.com/username/project.git (push)

6. Fetching a Specific Branch

You can fetch a specific branch from a remote:

git fetch origin main

This retrieves information associated with the main branch from origin.

7. Fetch All Remotes

If your repository has multiple remotes, you can fetch from all of them:

git fetch --all

This is useful when you work with multiple remote repositories.

8. Fetch Does Not Automatically Merge

One important feature of git fetch is that it does not automatically merge the fetched changes into your current branch.

git fetch origin

After fetching, you can inspect the changes and decide how to integrate them.

9. Git Fetch vs Git Pull

git fetch git pull
Downloads remote changes Downloads and integrates remote changes
Does not directly update the current branch Can update the current branch
Useful for reviewing changes first Useful for synchronizing a branch

10. Remote-Tracking Branches

After fetching, Git updates remote-tracking branches such as:

origin/main
origin/feature-login

These references represent the state of the corresponding branches as last known from the remote.

11. Viewing Remote Branches

Use:

git branch -r

Example:

origin/main
origin/develop
origin/feature-login

The -r option displays remote-tracking branches.

12. Viewing Local and Remote Branches

Use:

git branch -a

This displays both local branches and remote-tracking branches.

* main
  feature-login
  remotes/origin/main
  remotes/origin/feature-login

13. Fetching Before Reviewing Changes

A common workflow is:

git fetch origin
git log main..origin/main

The second command can show commits that exist on origin/main but not on the local main branch.

14. Comparing Local and Remote Branches

After fetching, you can compare your local branch with the remote-tracking branch.

git diff main origin/main

This helps you inspect file-level differences before integrating changes.

15. Checking Remote Commits

After fetching, use:

git log origin/main

This allows you to inspect the commit history currently known for the remote-tracking branch.

16. Fetching Changes from GitHub

If your Git repository is connected to GitHub, you can fetch updates using:

git fetch origin

The remote information is downloaded to your local repository without automatically changing the files in your current working branch.

17. Fetching from Upstream

A repository may also have a remote named upstream.

git fetch upstream

This is commonly used when working with a fork and an original repository.

18. Fetch Prune

Remote branches that were deleted from the server can leave stale remote-tracking references locally.

You can clean up stale references with:

git fetch --prune

You can also combine it with a remote:

git fetch origin --prune

19. What is Pruning?

Pruning removes local remote-tracking references for remote branches that no longer exist on the remote repository.

For example, if a remote branch is deleted:

origin/old-feature

a prune operation can remove its stale local reference.

20. Fetch and Working Files

Fetching does not normally replace the files in your current working directory with the remote branch's files.

For example:

git fetch origin

You can inspect the fetched information before deciding whether to merge, rebase, or otherwise integrate it.

21. Fetch Before Merge

You can explicitly fetch remote changes and then merge them:

git fetch origin
git merge origin/main

This separates the retrieval step from the integration step.

22. Fetch Before Rebase

Fetching can also be used before rebasing local work onto a remote branch:

git fetch origin
git rebase origin/main

This gives you explicit control over when the remote information is retrieved and how your local commits are integrated.

23. Fetch with Uncommitted Changes

Because fetching does not directly modify the current working files, it is generally useful even when you have local work that has not been committed.

git status
git fetch origin

However, you should still review your local state before performing a later merge or rebase.

24. Fetch in Team Projects

In a team project, developers can use fetch to see whether teammates have pushed new commits.

git fetch origin
git log HEAD..origin/main

This helps you inspect changes before integrating them into your current work.

25. Common Git Fetch Commands

git fetch
git fetch origin
git fetch origin main
git fetch --all
git fetch --prune
git fetch origin --prune

Choose the command according to which remote and branches you need to update.

26. Common Git Fetch Problems

  • Remote name is incorrect.
  • Remote repository URL is incorrect.
  • Authentication fails.
  • Internet connection is unavailable.
  • The requested branch does not exist on the remote.
  • You are working with the wrong repository.

Check the remote configuration with:

git remote -v

27. Fetch vs Pull Example

Using fetch:

git fetch origin
git log main..origin/main
git diff main origin/main
git merge origin/main

Here you inspect the remote changes before integrating them.

Using pull:

git pull origin main

The pull command performs retrieval and then an integration step.

28. Complete Git Fetch Workflow

git status

git fetch origin

git branch -r

git log main..origin/main

git diff main origin/main

git merge origin/main

This workflow gives you an opportunity to inspect remote changes before integrating them into the local branch.

29. Git Fetch Checklist

  • Check the current repository.
  • Verify the remote name.
  • Fetch the intended remote.
  • Review remote-tracking branches.
  • Inspect new commits when needed.
  • Compare local and remote branches.
  • Choose merge or rebase when ready.
  • Use prune to clean stale remote-tracking references when appropriate.

30. Summary of Git Fetch

The git fetch command retrieves information from a remote repository without directly integrating those changes into the current local branch.

git fetch origin

A useful workflow is:

git fetch origin
git log main..origin/main
git diff main origin/main
git merge origin/main

Using fetch gives you more control because you can inspect remote changes before integrating them.

📌 Key Points

  • git fetch retrieves changes from a remote repository.
  • It updates remote-tracking branches.
  • It does not automatically merge changes into the current branch.
  • git fetch origin fetches from the origin remote.
  • git fetch --all fetches from all configured remotes.
  • git fetch --prune removes stale remote-tracking references.
  • git branch -r displays remote-tracking branches.
  • git branch -a displays local and remote branches.
  • Fetched changes can be inspected using git log and git diff.
  • After fetching, you can merge or rebase the remote changes.
  • git fetch gives more control than directly using git pull.

🧠 Quick Quiz

Question: What is the main purpose of git fetch?