Guide
Git Merge Master Into Branch Without Losing Work
Fetch the remote master branch, verify your feature branch and working tree, merge origin/master, resolve conflicts, test the result, and push safely.
How should you prepare the branch?
Start with a clean tree. Confirm that you are on the feature branch, save every current change as a commit or stash, then fetch the remote before merging anything.
- Run
git statusand read the first line. It should name your feature branch, not master. - If files are modified, commit them or run
git stash push -u -m "before master merge"to include untracked files. - Fetch updated branch and commit data without changing the checked-out files:
git fetch origin- Verify the remote branch exists with
git branch -r --list origin/master.
Stop if the last command prints nothing. The repository may use main, in which case inspect git remote show origin and substitute origin/main; running git checkout main is unnecessary when the goal is merely to update a feature branch.
Fetching first matters because a local master branch can be weeks behind the server. The git-fetch documentation explains that fetch updates remote-tracking branches while leaving the working tree alone.
Which Git merge command updates the feature branch?
Merge from the feature branch. With the feature branch checked out, run git merge origin/master; Git joins the fetched master history into the current branch and preserves its existing commits.
- Confirm the current branch again with
git branch --show-current. - Preview commits arriving from master:
git log --oneline --decorate HEAD..origin/master- Start the merge:
git merge origin/master- If Git reports
Already up to date., no new master commits need merging. - If the command succeeds, run
git statusand inspect the graph withgit log --graph --oneline --decorate -20.
That is the safe git merge master into branch direction: the checked-out branch receives changes from the named source. Do not switch to master and merge the feature branch there, because that performs the opposite operation and can put unfinished code into master.
Git may fast-forward when the feature branch has no unique commits; otherwise it creates a merge commit under the default configuration. This does not duplicate the underlying commits. It adds a commit with two parents, recording where the histories join, as described by the git-merge manual.
If project policy requires an explicit merge node, use git merge --no-ff origin/master. I would not add --no-ff unless the repository's history rules call for it.
How do you resolve a Git merge conflict?
Resolve each marked file manually. Edit every conflict block into the final code you want, stage the corrected files, then finish the interrupted merge with git commit.
- List unresolved paths with
git statusorgit diff --name-only --diff-filter=U. - Open each file and find markers beginning with
<<<<<<<,=======, and>>>>>>>. - Keep or combine the correct content, then delete all three marker lines.
- Stage each resolved file with
git add path/to/file. - Check that no unresolved paths remain:
git diff --check
git diff --name-only --diff-filter=U- Complete the merge with
git commit. Keep the generated message unless the project has a specific convention.
Here, “ours” means the feature branch currently checked out, while “theirs” means origin/master, a detail people routinely reverse under pressure. To accept one complete version of a conflicted file, use git checkout --ours path/to/file or git checkout --theirs path/to/file, inspect it, and then stage it. Those commands discard the other side's content for that file, so they are blunt tools rather than general conflict resolution.
Git records unresolved paths in its index stages until they are added, and the merge-conflict section of the Git manual documents the marker and index behavior. Do not push while git status still says You have unmerged paths.
How do you verify or undo the completed merge?
Test before pushing. Run the repository's normal build and test commands, inspect the resulting diff and history, then push the feature branch only after both code and merge direction are correct.
- Run the project's formatter, compiler, unit tests, and integration tests as applicable.
- Inspect the merge commit with
git show --stat --summary HEAD. - Confirm that master is now an ancestor of the feature branch:
git merge-base --is-ancestor origin/master HEAD
echo $?An exit status of 0 confirms that the fetched master history is included. On PowerShell, inspect $LASTEXITCODE instead of running echo $?.
- Push without rewriting existing remote commits:
git push origin HEADRecovery depends on state. If conflicts are still open, run git merge --abort; Git attempts to restore the pre-merge state, although pre-existing uncommitted changes can prevent a perfect reconstruction. If the merge commit exists locally but has not been pushed, inspect git reflog, confirm the previous tip, and run git reset --hard ORIG_HEAD only after saving anything created since the merge.
A pushed merge should normally be reversed with git revert -m 1 <merge-commit> rather than reset and force-push, because revert adds a new commit without rewriting shared history. The -m 1 option selects the feature branch side as the mainline, a behavior covered by the git-revert reference. For the broader choice between preserving a merge and replaying commits, see Git merge versus rebase.
FAQ
- Does merging master into a feature branch duplicate commits?
- No. Git keeps the existing commit objects and may add one merge commit with two parents; repeated changes in a log view usually reflect graph presentation, rebasing, cherry-picking, or squash history rather than the merge copying commits.
- Should I merge origin/master or my local master?
- Merge
origin/masteraftergit fetch originwhen you want the latest fetched server state. A local master branch is safe only if you have separately updated and verified it, and switching branches adds an unnecessary place to make a directional mistake. - Can I use rebase instead of merging master?
- Yes, if rewriting the feature branch's commits is acceptable. Rebase produces a straighter history but changes commit IDs, so avoid rebasing commits that teammates already use unless the team has agreed on the force-push workflow.