How to merge a remote branch locally
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Merging a remote branch locally means fetching the latest remote state, checking out the local branch you want to update, and then merging the remote-tracking branch into it. The important part is understanding that you do not merge “the remote” directly. You merge the local reference Git stores for that remote branch, usually something like origin/feature-name.
Fetch the Remote State First
Before merging, update your local view of the remote repository.
This downloads the latest refs from origin without changing your working branch. After that, Git knows about the current remote branch tips such as origin/main or origin/feature-x.
You can inspect them with:
This is a useful habit because it separates “get the latest remote information” from “change my local branch.”
Switch to the Branch That Should Receive the Merge
Next, check out the local branch that should absorb the remote changes.
Or, with older Git:
This step matters because git merge merges into the branch you currently have checked out. If you are on the wrong branch, Git will happily merge into the wrong target.
Merge the Remote-Tracking Branch
Now merge the remote branch reference into your current branch.
This creates a merge using the exact remote branch state you fetched earlier. If there is no conflict and the histories diverged, Git creates a merge commit. If your current branch is strictly behind the remote-tracking branch and no local divergence exists, Git may perform a fast-forward instead.
Example Workflow
Suppose you want to merge a coworker's remote branch into your local main branch:
That is the core workflow. It is short because most of the complexity in Git merging comes from branch choice and conflict handling, not from the command sequence itself.
Resolve Conflicts If They Appear
If Git reports conflicts, it stops the merge so you can resolve them manually. A typical conflict workflow is:
- Open the conflicted files.
- Edit the conflict markers away.
- Stage the resolved files.
- Complete the merge commit.
Do not try to continue working as if the merge succeeded automatically. Until the conflicts are resolved and committed, the repository is in a partial merge state.
Merge Versus Track the Remote Branch
Sometimes developers really want a local branch that tracks the remote branch, not a merge into another branch. In that case, create a local branch first:
That creates a local branch connected to the remote branch. From there, you can inspect it, test it, or later merge it into another local branch. This is often cleaner than merging immediately if you first want to review the changes.
Rebase Is a Different Operation
Many articles mix merge and rebase together. They are not the same thing.
git merge origin/feature-xpreserves the branch history structure.git rebase origin/feature-xrewrites your current branch on top of another history.
If your goal is literally to merge a remote branch locally, use merge. Rebase may be useful in some workflows, but it answers a different question.
Keep the Remote Name Explicit
In teams with multiple remotes, always write the remote name explicitly. origin/feature-x is clearer and safer than mentally assuming every branch comes from origin.
That is especially important when working with forks, upstream repositories, or review remotes.
Common Pitfalls
- Running
git mergebeforegit fetchand merging stale remote-tracking information. - Forgetting that the current local branch is the merge target.
- Merging
origin/branchwhen you actually wanted to create a local tracking branch first. - Treating merge and rebase as interchangeable when they have different history effects.
- Trying to ignore conflicts and continue working before the merge is properly resolved.
Summary
- To merge a remote branch locally, first fetch the remote, then switch to the target local branch, then merge the remote-tracking branch.
- '
origin/branch-nameis a local reference to the remote branch state you fetched.' - Conflict resolution is part of the merge workflow, not an edge case to skip.
- Create a local tracking branch first if you want to inspect the remote branch before merging it.
- Keep merge and rebase conceptually separate so the command choice matches the history you want.

