HOWTO · Git

Difference Between Git Merge Master and Git Merge Origin/Master

Explore the differences between git merge master and git merge origin/master in this comprehensive guide. Understand when to use each command to enhance your Git workflow. Learn how to effectively manage your branches and collaborate seamlessly with your team. This article provides clear explanations and examples to help you navigate version control with confidence.

On this page

git merge integrates the commit named by its argument into the branch currently checked out. It does not switch branches. The difference between git merge master and git merge origin/master is the source commit: one is named by a local branch, while the other is named by a local remote-tracking ref.

How git merge origin/master works

origin/master records the last fetched state of the remote branch named master at origin. It is stored locally, so it does not update just because someone pushed new commits. Run git fetch origin to update remote-tracking refs from that remote. Fetching downloads and records commits; it does not merge them into your current branch or move your local master branch.

To incorporate the fetched commits into feature, check out the intended receiving branch, fetch, and then merge:

git switch feature
git fetch origin
git merge origin/master

These commands are an unexecuted illustration; their result depends on your repository’s history. Fetch before merging so origin/master reflects the remote state you want. Without a fetch, Git uses the older value already recorded locally. If you are unsure where changes will go, check the current branch with git branch --show-current before merging.

If the feature branch has no commits beyond the source commit, Git may fast-forward its branch pointer. If both branches have new commits, Git can create a merge commit; overlapping edits can require conflict resolution. A fast-forward is one possible result, not a guarantee.

How git merge master works

master names the local branch in your repository. It may contain commits you have not pushed, or it may lag behind the remote if you have not updated it. To merge that local branch into feature, switch to the receiving branch first:

git switch feature
git merge master

This does not fetch from origin or update local master. Git integrates the commit currently named by local master into the checked-out branch. Depending on the commit graph and file changes, the result can be a fast-forward, a merge commit, or conflicts. The command sequence is an unexecuted illustration; no particular output can be assumed for another repository.

Choose the merge source that matches your goal

Use git merge master when the commits you want are already on your local master branch. Use git fetch origin followed by git merge origin/master when you want the latest fetched state of the remote’s master branch, whether or not local master has been updated to match it. In either workflow, check out the branch that should receive the changes before merging.

The two names can point to different commits. Local master might contain your unpushed work, while origin/master might contain commits fetched from collaborators. Fetch updates the remote-tracking ref, not local master. If you want to compare the sets of commits represented by these names, remember that Git considers commit ancestry, not just branch labels. The following small model treats each set as the commits reachable from a ref and lists candidates not already reachable from feature:

Run this small model with representative commit IDs:

refs = {
    "master": {"base", "local-change"},
    "origin/master": {"base", "remote-change"},
}
feature = {"base", "feature-change"}

for ref, reachable in refs.items():
    candidates = sorted(reachable - feature)
    print(f"{ref}: {', '.join(candidates)}")
master: local-change
origin/master: remote-change

This set model is only a compact way to compare the example names; Git uses the actual commit graph to decide which commits a merge needs. If local master should track the remote, update it separately by checking it out and merging origin/master. That is a different receiving-branch choice from merging into feature.

Common questions about these merge commands

Can I merge both master and origin/master? Yes. Each names a source commit, and Git integrates commits not already reachable from the current branch. The refs may point to the same commit, or one may contain much of the other’s history, so the second merge may add nothing. They may also contain distinct commits. Inspect the history and choose deliberately; merging both without checking can bring in changes you did not intend.

Does git merge origin/master update local master? No. A merge updates the branch currently checked out. git fetch origin updates the remote-tracking ref; merging it while on feature integrates those commits into feature.

Why did my merge omit a recent remote commit? The local origin/master ref may be stale. Fetch from the correct remote, then inspect the source ref before merging. Also confirm that the commit is on the remote branch named master; a different remote or branch requires a different ref.