How do I resolve a git merge conflict?

CS Fundamentals 2 min read
Short answer

Git marks the conflicting region with <<<<<<<, ======= and >>>>>>>. Edit the file so it contains what you want, delete the markers, git add it, and commit.

text
<<<<<<< HEAD
const timeout = 30;
=======
const timeout = 60;
>>>>>>> feature-branch
  • Between <<<<<<< and ======= is your version (the branch you are on).
  • Between ======= and >>>>>>> is theirs (the branch being merged in).

Edit the region to the correct final content — which may be one side, the other, or a combination — and delete all three marker lines.

bash
git add conflicted-file.js
git commit           # git pre-fills a merge message

#Finding them all

bash
git status                    # lists unmerged paths
git diff --name-only --diff-filter=U

#Taking one side wholesale

bash
git checkout --ours path/to/file      # keep your version
git checkout --theirs path/to/file    # keep theirs
git add path/to/file

Careful during a rebase: "ours" and "theirs" swap meaning, because your commits are being replayed onto the other branch. Check with git status before assuming.

#Backing out

bash
git merge --abort
git rebase --abort

Returns everything to how it was before you started.

#Reducing how often it happens

  • Merge or rebase from main frequently, so branches never drift far apart.
  • Keep branches short-lived and focused.
  • Agree on formatting and enforce it automatically — most gratuitous conflicts are whitespace.

#Turn on rerere

bash
git config --global rerere.enabled true

"Reuse recorded resolution" remembers how you resolved a conflict and replays it automatically next time. It pays for itself on the first long-running rebase.