Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

Why Conflicts Happen

Git combines changes line by line. When the two branches changed different parts of a file, it merges them without asking. It stops only when it cannot decide:

  • Same lines: both branches changed the same lines, or lines right next to each other.
  • Edit and delete: one branch edited a file that the other deleted.
  • Same new file: both branches added a file at the same path with different content.

Find the Conflicted Files

git status

Conflicted files appear under unmerged paths, marked "both modified" or similar. Files that merged cleanly are already staged. For a plain list of conflicted files only:

git diff --name-only --diff-filter=U

Read the Conflict Markers

Open a conflicted file. Git has written both versions into it:

<<<<<<< HEAD
timeout = 30
=======
timeout = 60
>>>>>>> feature/timeout

The part between the first marker and the line of equals signs is your side, the branch you are on. The part below it, up to the closing marker, comes from the branch you are merging. To also see the original lines from before either change, which makes many conflicts easier to judge, switch on a third section:

git config --global merge.conflictStyle diff3

The original lines then appear after an extra marker of vertical bars. The zdiff3 style does the same but leaves out lines that both sides share.

Resolve and Finish

  1. Edit the file so it contains the result you want: one side, the other, or a mix.
  2. Delete all the marker lines.
  3. Stage the file with git add. That tells Git the conflict in it is resolved.
  4. Repeat for every conflicted file, then finish the merge.
git add config/app.ini
git merge --continue

git commit works in place of --continue. Before you stage, git diff --check warns about leftover markers, and running the tests catches results that look right but are not.

Take One Side Whole

For files where one version is the right one as a whole, such as a generated lock file, take it without editing:

git checkout --ours package-lock.json
git checkout --theirs package-lock.json
git add package-lock.json

Run one of the first two lines, not both. git restore --ours and --theirs do the same. --ours is the branch you are on and --theirs is the branch you are merging in. For a lock file, regenerating it with your package manager after the merge is often the better fix.

Ours and Theirs Swap During a Rebase

A rebase replays your commits on top of the other branch. While it runs, --ours means the branch you are rebasing onto, and --theirs means your own commits being replayed. Check the markers' labels before you pick a side.

Abort and Start Over

git merge --abort

This returns everything to the state before the merge. It works cleanly when you started the merge with no uncommitted changes, which is a good habit for that reason. During a rebase or cherry-pick, use git rebase --abort or git cherry-pick --abort.

Conflicts During Rebase and Cherry-Pick

The steps are the same: edit, remove the markers, stage. Only the command to go on differs:

git rebase --continue
git cherry-pick --continue

A rebase can stop at several commits in a row, one conflict at a time. More on copying commits is in Git cherry-pick: copy a commit to a branch.

Merge Tools and Fewer Conflicts

git mergetool opens each conflicted file in a configured tool, such as vimdiff, with the versions side by side. To get fewer conflicts in the first place, merge main into long-running branches often and keep commits small. The merge itself is covered in git merge: combine branches in Git.

Was this answer helpful?
Loading...