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.
Before you integrate
Section titled “Before you integrate”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.
- Select the feature branch and use Fetch origin.
- In Current Branch, choose the action to merge
maininto the current branch, if your team’s policy uses merges. - If Desktop reports conflicts, open the affected files in your configured editor.
- Keep the correct content from both sides, save, and mark conflicts resolved using the interface.
- 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 status --short --branchgit fetch origingit merge origin/main# If a conflict occurs: inspect affected files and edit carefully.git statusgit diff --checkgit add path/to/resolved-filegit commit# If you have not resolved and need to cancel the merge:# git merge --abortgit merge --abort is only for an in-progress merge; review its consequences first, especially if you had local changes. The path/to/resolved-file path is a placeholder.
Reading conflict markers
Section titled “Reading conflict markers”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.
Recover safely
Section titled “Recover safely”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:
mainis an example. Usemasteror your repository’s actual default branch where applicable. See Git terminology.
