Merge Conflict in Git: The Simplest Resolution Workflow
Share
TL;DR: Keep your working tree clean, rebase first, resolve file by file, run tests, then continue. If things go sideways, abort and try again. Below is a reliable, text-first flow you can memorize.
Mental model
A conflict means two commits changed the same lines or adjacent context. Your job is to decide which change wins or how to combine them, then produce a clean result.
Minimal workflow
# make sure you have no local changes you cannot stash
git status
# get upstream changes
git fetch origin
# rebase your branch onto the latest main
git rebase origin/main
# resolve conflicts as they appear
# open each file, search for <<<<<<< ======= >>>>>>>
# edit to a single correct version
# mark resolved files
git add <file>
# continue the rebase
git rebase --continue
# run tests and linters
npm test # or ./gradlew test, etc.
Resolving files
Look for conflict markers. Decide which side to keep or how to merge them into a single version. Remove the markers completely.
<<<<<<< HEAD
const port = 3000;
=======
const port = 8080;
>>>>>>> origin/main
Final:
const port = 8080; // choose one and move on
Avoiding conflicts next time
- Pull and rebase before starting new work.
- Keep PRs small and focused.
- Move unrelated refactors to a separate change.
Useful aliases
git config --global alias.co checkout
git config --global alias.st status
git config --global alias.rb "rebase"
FAQ
Rebase or merge
Rebase keeps history linear and reduces future conflicts. Use merge if your team policy prefers it.
How do I abort safely
Run git rebase --abort or git merge --abort to return to the previous state. Commit or stash your edits first.
I committed conflict markers by accident
Revert that commit or fix the file and commit again. Add a pre-commit hook that prevents committing markers.
Cast this in your project
- Merge Conflict T-shirt for the Git warriors.
- Git Flashcards to practice core commands and flows.
Photo by Vera Arsic: https://www.pexels.com/photo/man-and-woman-wearing-brown-leather-jackets-984950/