git pull === git fetch + git merge
What Is the Difference Between ‘Git Pull’ and ‘Git Fetch’?
Demystifying the key differences between two essential Git commands, plus how to git pull and git fetch in GitHub Desktop.
Git pull vs Git fetch: A Primer on the Difference
Version control is a vital aspect of any software development process, and Git has established itself as one of the most popular and widely-used tools for managing code changes over time. Understanding the core Git commands is critical for developers, but even for the experienced, the difference between git pull and git fetch can sometimes be a bit unclear. In this article, we will explore these two commands and their applications, both in the command line and with GitHub Desktop.
First, let’s provide a brief overview of the two commands:
git pull: This command retrieves (fetches) changes from a remote repository and merges them into your local branch. Essentially, it is a combination ofgit fetchandgit merge.git fetch: This command retrieves changes from a remote repository but does not merge them. Instead, it updates the remote-tracking branches, allowing you to review and merge the changes manually.
To provide a clearer understanding of when you would use each, we’ll dive deeper into the mechanics of each command in the next section.
The git pull command
git pull is the more commonly used command, as it automates the process of fetching and merging changes from a remote repository. When you execute git pull, Git does two things:
- Fetches the changes from the remote repository
- Merges the changes into your local branch
Here’s an example:
git pull origin main
This command fetches the changes from the main branch of the remote repository origin and merges them into your current local branch.
While git pull is convenient, it may not always be the best choice, particularly when you're dealing with a large number of changes or working in a team environment. This is where git fetch comes in handy.
The git fetch command
The git fetch command allows you to fetch changes from a remote repository without immediately merging them. This provides you with the opportunity to review the changes and decide how to proceed.
To fetch changes, simply run:
git fetch origin
This command retrieves the changes from the remote repository origin and updates your remote-tracking branches, but it does not merge them into your local branch.
To view the differences between your local branch and the fetched remote branch, you can use the git diff command:
git diff main origin/main
Once you’re satisfied with the changes, you can manually merge the remote branch into your local branch:
git merge origin/main
How To Git Pull and Git Fetch in GitHub Desktop
Now that we’ve covered the command-line usage of git pull and git fetch, let's take a look at how these commands translate to the GitHub Desktop application.
Git Pull in GitHub Desktop
To perform a git pull in GitHub Desktop, follow these steps:
- Open your repository in GitHub Desktop.
- Click on the “Fetch origin” button in the top right corner to fetch the changes.
- If there are changes available, the button will change to “Pull origin”. Click on it to merge the changes into your local branch.
Git Fetch in GitHub Desktop
To perform a git fetch in GitHub Desktop, follow these steps:
- Open your repository in GitHub Desktop.
- Click on the “Fetch origin” button in the top right corner. This will fetch the changes but not merge them.
Merging a Remote Branch in GitHub Desktop
Once you’ve done git fetch, you might want to merge branches next:
- To view the differences between your local branch and the remote branch, click on the “History” tab in the left sidebar.
- In the “History” tab, you will see a list of commits. Commits fetched from the remote repository will be displayed with an icon indicating that they have not been merged into your local branch.
- Review the changes by clicking on each commit to see the details, including the files changed and the line-by-line differences.
- Once you’re satisfied with the changes, click on the “Branch” menu in the top toolbar, and then select “Merge into current branch.”
- In the “Choose a branch” dropdown, select the remote branch you want to merge, and then click the “Merge” button.
5 Key Differences Between Git Pull and Fetch
- Combination of commands:
git pullis a combination ofgit fetchandgit merge. It fetches changes from the remote repository and automatically merges them into your local branch. In contrast,git fetchonly fetches changes from the remote repository and updates your remote-tracking branches without merging the changes. - Merging behavior: When you use
git pull, Git automatically merges the fetched changes into your local branch, potentially causing conflicts if there are differences between your local and remote branches. On the other hand,git fetchallows you to review the changes before deciding to merge them, giving you more control over the merging process and the opportunity to resolve conflicts more effectively. - Safety and control:
git fetchis considered a safer option, especially in a team environment, because it does not automatically merge changes. This allows you to review and verify the changes fetched from the remote repository before merging them.git pull, while more convenient, can sometimes lead to unexpected conflicts and issues if used without caution. - Local branch updates: When using
git pull, your local branch is immediately updated with the fetched changes after the merge. In contrast, withgit fetch, your local branch remains unchanged until you manually merge the fetched changes. - Use case: The primary use case for
git pullis when you want a quick and easy way to fetch and merge changes from the remote repository into your local branch.git fetchis more appropriate when you need to review the changes before merging them, have multiple developers working on the same project, or want to maintain better control over the merging process.
Conclusion: git pull === git fetch + git merge
And that’s it! You now know the difference between git pull and git fetch, as well as how to use these commands in both the command line and GitHub Desktop. By understanding when to use each command, you can maintain better control over your codebase and improve collaboration within your team.
Remember, git pull is best for quickly fetching and merging changes, while git fetch is ideal for reviewing changes before deciding to merge them. By mastering these commands, you will be better equipped to handle any Git-related challenges that come your way.
But what if you need a git undo command? Well, in that case you’ll need to read my other article:
Happy coding! 🥳







