When working with Git, especially in collaborative projects, you’ll often need to bring in changes made by others. The safest way to do this is by using two core commands: git fetch
and git merge
.
Understanding the difference—and how to use them together—is key to maintaining a clean, conflict-free development workflow.
In this post, we’ll explain what these commands do, when to use them, and how to fetch and merge properly.
🤔 What’s the Difference Between git fetch
and git merge
?
git fetch
- Downloads new data from a remote repository (like GitHub), including branches and commits.
- Does not change your local working directory or files.
- Ideal for previewing changes before integrating them.
git merge
- Integrates fetched changes (or any branch) into your current working branch.
- May trigger merge conflicts if changes overlap.
📥 Step-by-Step: How to Fetch and Merge
1. Open Your Terminal or Git Bash
Navigate to your local project repository.
cd your-project-directory
2. Fetch Changes from the Remote Repository
git fetch origin
- This pulls in all the latest commits, branches, and tags from the
origin
remote. - You can also fetch a specific branch:
git fetch origin main
💡
origin
is the default name for the remote repository.
3. Merge the Fetched Branch into Your Local Branch
Switch to the branch where you want to apply the changes (e.g., main
or develop
):
git checkout main
Then merge the remote branch:
git merge origin/main
This merges the origin/main
(the remote version) into your local main
branch.
🔄 Visual Example
Let’s say your teammate pushed new commits to the main
branch. To bring those changes into your local project:
git fetch origin
git checkout main
git merge origin/main
Now your local main
branch includes all their updates.
⚠️ Handling Merge Conflicts
If there are conflicting changes, Git will pause the merge and notify you. You’ll need to:
- Open the conflicted files.
- Manually resolve the conflicts.
- Mark them as resolved:
git add <filename>
- Complete the merge:
git commit
✅ Pro Tip: Use git pull
(Carefully)
git pull
This command is a shortcut for:
git fetch + git merge
While convenient, it immediately attempts a merge, which can lead to conflicts if you’re not prepared. Using fetch
and merge
separately gives you better control.
🧠 Summary
Command | Purpose |
---|---|
git fetch | Downloads changes without applying them |
git merge | Integrates changes into your branch |
git pull | Fetch + Merge in one step |
🚀 Conclusion
Using git fetch
and git merge
separately gives you visibility and control over what you bring into your project. It’s the safest and most professional way to sync your work with others—especially when working in teams or managing complex codebases.
By mastering this simple but powerful combo, you’ll avoid surprises and keep your Git workflow smooth and predictable.