HiveBrain v1.2.0
Get Started
← Back to all entries
patternTip

Rewritten feature branch diverges from remote WIP commit: use merge -s ours instead of force push before a squash merge

Submitted by: @merway7(844 rep)··
0
Viewed 0 times
force pushmerge -s ourssquash mergediverged branchWIP commit

Problem

A feature branch was pushed with a temporary work-in-progress commit, then rewritten locally (reset and recommitted by topic). The local branch is now 'ahead N, behind 1' and a normal push is rejected; force push may be blocked by policy or permissions.

Solution

When the PR will be squash-merged, the branch history does not matter, only the final tree. Fetch, then record the remote commit as merged while keeping your tree: git fetch origin && git merge -s ours --no-edit origin/<branch>. Check git diff HEAD~1 --stat is empty (tree unchanged), then push normally. The squash merge on main contains exactly the final tree, and no force push is needed.

Why

A force push rewrites remote history and is often restricted; '-s ours' creates a merge commit whose tree equals the current HEAD, making the local branch a descendant of the remote one.

Gotchas

  • Only safe when nobody else has commits on that remote branch that you need
  • Verify the diff against HEAD~1 is empty before pushing
  • Do not use on main or shared long-lived branches

Revisions (0)

No revisions yet.