git
version control
remote branch
merge
tutorial

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.

bash
git fetch origin

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:

bash
git branch -r

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.

bash
git switch main

Or, with older Git:

bash
git checkout main

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.

bash
git merge origin/feature-x

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:

bash
git fetch origin
git switch main
git merge origin/feature-login

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:

  1. Open the conflicted files.
  2. Edit the conflict markers away.
  3. Stage the resolved files.
  4. Complete the merge commit.
bash
git add path/to/file
git 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:

bash
git switch -c feature-login --track origin/feature-login

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.

  1. git merge origin/feature-x preserves the branch history structure.
  2. git rebase origin/feature-x rewrites 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 merge before git fetch and merging stale remote-tracking information.
  • Forgetting that the current local branch is the merge target.
  • Merging origin/branch when 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-name is 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.

Course illustration
Course illustration

All Rights Reserved.