How to merge master branch into main
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Merging master into main is just a normal Git branch merge, but it usually happens during branch renaming, repository cleanup, or migration to a new default branch. The safe workflow is to update your local refs, switch to main, merge master into it, resolve any conflicts, and then push the updated main branch.
Basic Merge Workflow
Start by fetching the latest remote state:
Then switch to main:
Make sure main is current:
Now merge master into main:
If there are no conflicts, push the result:
That is the normal answer when both branches already exist locally.
If Local Branches Are Missing or Out of Date
Sometimes you only have remote-tracking branches or stale local copies. In that case, create or refresh local branches first:
The point is to avoid merging outdated local history by accident.
Understanding the Direction of the Merge
"Merge master into main" means:
- '
mainis the checked-out destination branch' - '
masteris the source branch whose commits are being integrated'
So the important command is:
If you do the reverse, you are updating master, not main.
Handling Conflicts
If Git reports conflicts, inspect them:
Open the conflicting files, resolve the merge markers, then stage the resolutions:
Complete the merge:
In many repositories, the merge commit message will already be prepared for you. If the repository enforces linear history, you may need a rebase-based workflow instead of a merge commit.
Fast-Forward Versus Merge Commit
If main has not diverged from master, Git may perform a fast-forward merge. That simply moves the main branch pointer forward.
If both branches have unique commits, Git creates a merge commit unless you choose another strategy. Both outcomes are normal. The correct one depends on repository history and policy.
When Renaming Is the Real Goal
Sometimes the real goal is not to keep both branches forever, but to move the project from master to main. In that case, after confirming main contains everything needed, teams often:
- update the remote default branch
- update CI configuration
- retarget pull requests
- eventually delete the old
masterbranch
That operational cleanup is often more important than the merge command itself.
Common Pitfalls
The most common mistake is merging in the wrong direction. If master should feed into main, then main must be checked out during the merge.
Another mistake is forgetting to fetch first, which can cause you to merge stale local branches instead of the latest remote state.
People also assume a merge is enough during a branch-renaming migration. In reality, branch protection rules, CI jobs, GitHub settings, and local developer workflows may still reference the old default branch.
Finally, do not force-push unless you actually intend to rewrite shared history. A normal merge does not require that.
Summary
- To merge
masterintomain, check outmainand rungit merge master. - Fetch and update local branches first so you are merging the latest history.
- Resolve conflicts on
main, then push the result. - Be explicit about merge direction to avoid updating the wrong branch.
- If this is part of a default-branch migration, remember the repository settings and CI cleanup too.
Related reading
- How to merge remote changes at GitHub?
- How to merge specific files from Git branches
- How to merge two BST's efficiently?
- How to merge two sorted arrays in Swift?
- how to merge two sorted integer array in place using On time and O1 space cost
- How to migrate GIT repository from one server to a new one
- How to modify a pull request on GitHub to change target branch to merge into?
- How to modify existing, unpushed commit messages?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.