MediumConcept

How do you resolve a merge conflict in Git?

Technology:
Git
Experience:
Junior,
Mid-level
Topics:
Git,
Debugging

Quick answer

Open the conflicted files, decide what the final code should be between the conflict markers, remove the markers, run the tests, then stage the files and continue the merge or rebase.

Detailed explanation

A conflict happens when two branches change the same lines, or one deletes a file the other edited. Git pauses the merge and marks each conflicted region with <<<<<<<, ======= and >>>>>>>. git status lists the files that need attention.

Resolving is not choosing a side. Read both changes, understand why each was made, and write the code that keeps the intent of both. Editors and git mergetool show the two versions side by side, but the decision is yours.

After editing, build and run the tests, because a conflict-free file can still be logically broken. Then git add the resolved files and run git merge --continue or git rebase --continue. If it goes wrong, git merge --abort returns you to the state before the merge.

Example

text
<<<<<<< HEAD
const timeout = 5000;
=======
const timeout = Number(process.env.TIMEOUT_MS);
>>>>>>> feature/config

# resolved: keep the env variable, fall back to the old default
const timeout = Number(process.env.TIMEOUT_MS ?? 5000);
After editing: git add config.js && git merge --continue

Real-world example

Two developers update the same configuration file: one adds a new environment variable, the other changes a default timeout. The right resolution keeps both changes, which neither "accept ours" nor "accept theirs" would do.

Key points

  • git status shows conflicted files
  • Combine the intent of both changes, then remove the markers
  • Run tests before continuing
  • git merge --abort or git rebase --abort to start over

Common mistakes

  • Clicking "accept incoming" or "accept current" for the whole file without reading the other change.
  • Committing files that still contain conflict markers.
  • Skipping the tests after resolving, so a semantic conflict reaches main.

Follow-up questions

  • GitEasy

    What is Git and how is it different from GitHub?

    Git is a distributed version control system that tracks changes to files on your machine; GitHub is a hosting service for Git repositories that adds collaboration features such as pull requests, code review and CI.

    Intern 路 Junior 路 Git
  • GitEasy

    What is the difference between git fetch and git pull?

    git fetch downloads new commits from the remote without changing your working branch; git pull runs a fetch and then merges (or rebases) those commits into your current branch.

    Intern 路 Junior 路 Git
  • GitMedium

    What is the difference between git merge and git rebase?

    Both integrate changes from one branch into another: merge keeps both histories and joins them with a merge commit, while rebase rewrites your commits so they appear on top of the target branch, giving a linear history.

    Junior 路 Mid-level 路 Git
  • GitMedium

    What is the difference between git reset and git revert?

    git revert creates a new commit that undoes an earlier one and is safe on shared branches; git reset moves the branch pointer back to an earlier commit, effectively removing later commits from that branch鈥檚 history.

    Mid-level 路 Senior 路 Git
  • ReactEasy

    Why do we need keys in React lists?

    Keys give each list item a stable identity so React can match items between renders and correctly add, remove or reorder them without recreating or mixing up their state.

    Junior 路 Mid-level 路 Performance
  • CSSMedium

    How does CSS specificity work?

    Specificity is the score the browser uses to decide which rule wins: inline styles beat IDs, IDs beat classes/attributes/pseudo-classes, and those beat elements. When scores tie, the last rule in the source wins.

    Junior 路 Mid-level 路 CSS Layout