Skip to content

Merge conflicts without losing work

↑

CLI examples labeled Bash work in Git Bash on Windows 10 and Windows 11, or Bash on Linux, with Git installed. Bash variable assignments such as DEFAULT_BRANCH=main are shell syntax; use Git Bash for these examples rather than pasting them into PowerShell or Command Prompt.

A conflict occurs when Git cannot automatically combine two histories, often because two branches changed the same lines or one branch deleted a file that another edited. A conflict is a request for a decision, not a command failure that should be bypassed. Git does not know which behavior the team intends.

Run tests on your branch, save meaningful uncommitted changes and fetch the current target branch. Preserve an untouched copy of anything valuable before manipulating complex histories. Understand whether your team prefers merging upstream changes or rebasing private commits; rebasing rewrites commit IDs, so avoid rebasing branches other people already use.

  1. Select the feature branch and use Fetch origin.
  2. In Current Branch, choose the action to merge main into the current branch, if your team’s policy uses merges.
  3. If Desktop reports conflicts, open the affected files in your configured editor.
  4. Keep the correct content from both sides, save, and mark conflicts resolved using the interface.
  5. Complete the merge, run tests and Push origin to update the review branch.

GitHub Desktop may expose several resolution choices. Do not accept entire files blindly when both edits are necessary.

Git may insert <<<<<<<, ======= and >>>>>>> around conflicting variants. These are not valid code; edit the file to produce the desired combined behavior and remove the markers. Check dependencies, generated files and tests that could be affected by either branch.

If a conflict becomes confusing, stop and inspect git status. Consider requesting a teammate’s help rather than resetting the branch. Keep a backup of uncommitted work before performing destructive operations. Avoid git reset --hard and git push --force as routine “fixes”: they can remove edits or replace shared history.

Read GitHub’s conflict reference and continue with history and recovery.

Branch naming: main is an example. Use master or your repository’s actual default branch where applicable. See Git terminology.