‘git pull’ and ‘git fetch’ explained (with examples)

Updated: January 27, 2024 By: Guest Contributor Post a comment

Introduction

Understanding how to manage and synchronize code among multiple contributors is a foundational skill for modern developers. Git, the widely used version control system, offers powerful commands for collaborating on code. In this tutorial, we’ll dissect the differences and use cases for git pull and git fetch, complete with practical examples.

Getting Started

Before diving into the commands, let’s establish the basis we’re working on. Ensure you have Git installed on your system and have access to a Git repository. Initializing a repository can be done with git init, while cloning a remote repo is achieved through git clone.

Understanding Remote Repositories

When you work with Git, you often interact with remote repositories. These are versions of your project that are hosted on the internet or network somewhere, and they serve as important backups and collaboration points for your code.

To add a remote repository from your command line, use:

git remote add origin https://github.com/username/repo.git

This sets up a remote named ‘origin’ which points to your remote repository on GitHub.

Exploring ‘git fetch’

Starting with git fetch, this command is your insight into the changes posted to the remote repository without immediately merging them into your local branch.

Fetching information means you are retrieving new work done by others, updates to branches, and tags without changing your own working files. Let’s run through the basic use:

git fetch origin master

This command fetches the updates from the ‘master’ branch on the ‘origin’ remote repo, allows you to inspect them, and decide if you want to merge them.

Inspecting Changes

After fetching, you may want to review the changes before integrating them. Use:

git log HEAD..origin/master

This command shows you the commit history differences between your current HEAD and the fetched changes on the ‘origin/master’ branch.

The Role of ‘git pull’

Moving on to git pull, this command combines the actions of ‘fetch’ and ‘merge’ into one. It fetches the remote content and immediately attempts to merge it into the corresponding branch in your repository. Here’s a simple pull:

git pull origin master

This will fetch the updates from ‘origin/master’ and merge them directly into your local master branch.

Dealing with Merge Conflicts

When a git pull results in a merge conflict, you’ll need to resolve it. For instance, let’s say you have a conflict:

AUTO-MERGE failed
Fix conflicts and then commit the result.

To resolve this, open the files indicated by Git, look for the ‘<<<<<<‘, ‘======’, and ‘>>>>>>’ markers, decide how you want to integrate the changes, and then continue by committing the resolution.

Fetching Versus Pulling

While ‘git fetch’ and ‘git pull’ can be seen as similar, their outcomes are different. ‘git fetch’ is a safe way to review changes before integration, while ‘git pull’ is a quicker way of updating your repository if you’re ready to incorporate the new commits immediately. Each has its place in a developer’s toolkit depending on the context of the workflow.

Using Fetch to Manage Multiple Branches

When dealing with multiple branches, ‘git fetch’ is particularly useful. Run:

git fetch --all

This command fetches updates for all branches from the remote, leaving your current branch unaffected.

Strategies for Pulling

git pull has a few strategies to control merge behaviors:

git pull -r

By adding the -r flag, you’re telling Git you prefer a rebase instead of a merge.

Advanced Git Fetch and Pull Techniques

As your knowledge grows, so do the techniques at your disposal:

Fetching by Tag

git fetch origin tag <tagname>

This command isolates fetching a specific tag, avoiding retrieving all branch updates.

Pruning References

git fetch -p

The -p or –prune option removes any remote-tracking references that no longer exist on the remote.

Conclusion

In conclusion, both git fetch and git pull are invaluable tools in a developer’s version control strategy. Understanding when and how to use them effectively can lead to a more efficient and controlled development process.