CodecPath

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.

CodecPath editors4 min read
Clean featurebranchWork committed orstashedFetch originRefreshes origin/masterMerge origin/masterJoins histories on thefeature branchResolve conflictsStages the intendedfinal codeTest and pushPublishes the verifiedbranch
The safe update sequence keeps the merge direction explicit and leaves recovery points before anything reaches the remote.

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.

  1. Run git status and read the first line. It should name your feature branch, not master.
  2. If files are modified, commit them or run git stash push -u -m "before master merge" to include untracked files.
  3. Fetch updated branch and commit data without changing the checked-out files:
Bash
git fetch origin
  1. 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.

  1. Confirm the current branch again with git branch --show-current.
  2. Preview commits arriving from master:
Bash
git log --oneline --decorate HEAD..origin/master
  1. Start the merge:
Bash
git merge origin/master
  1. If Git reports Already up to date., no new master commits need merging.
  2. If the command succeeds, run git status and inspect the graph with git 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.

  1. List unresolved paths with git status or git diff --name-only --diff-filter=U.
  2. Open each file and find markers beginning with <<<<<<<, =======, and >>>>>>>.
  3. Keep or combine the correct content, then delete all three marker lines.
  4. Stage each resolved file with git add path/to/file.
  5. Check that no unresolved paths remain:
Bash
git diff --check
git diff --name-only --diff-filter=U
  1. 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.

  1. Run the project's formatter, compiler, unit tests, and integration tests as applicable.
  2. Inspect the merge commit with git show --stat --summary HEAD.
  3. Confirm that master is now an ancestor of the feature branch:
Bash
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 $?.

  1. Push without rewriting existing remote commits:
Bash
git push origin HEAD

Recovery 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/master after git fetch origin when 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.

Official software

Related guides