Resources /
Blog

Git Merge Conflicts in Salesforce: How to Resolve Them

Submit your details to get a book

9
Min Read
Resources /
Blog

Git Merge Conflicts in Salesforce: How to Resolve Them

Download

Submit your details to get a book

9
Min Read
Illustration of a branching Git graph on a stepped platform, linked to code, copy, sync and list tiles
Illustration of a branching Git graph on a stepped platform, linked to code, copy, sync and list tiles

A Git merge conflict happens when two branches change the same lines of a file and Git can't tell which version to keep. To resolve it, open the flagged file, find the conflict markers, keep the code you want, delete the markers, then stage and commit the merge.

You can do that in a text editor, a merge tool, the command line or a pull request, and each method is covered below. After that come the usual causes and the habits that prevent them.

What Is a Merge Conflict?

Git usually combines changes from different branches without any help. When edits overlap in the same file, though, it pauses the merge and leaves the choice of which change to keep to you.

Merge Conflict in Salesforce

Git marks the conflicting sections, so the trouble spots are easy to find. Fixing them is still a manual job, and on a busy release that can slow deployments down.

Git can stop a merge in two places. Before the merge starts, Git refuses to run if you have uncommitted local changes to files the merge would touch; commit or stash them first. During the merge, Git reports "CONFLICT (content): Merge conflict in <file>" and "Automatic merge failed; fix conflicts and then commit the result."

Why Resolve Merge Conflicts?

Leave a conflict unresolved and it tends to cause more trouble later. In a Salesforce DevOps process, clean, conflict-free code is what keeps deployments and CI/CD processes running.

On a team project, one open conflict can block development and hold up everyone waiting on that branch. Left alone, conflicts also break code, which shows up as failed builds and bugs.

There's a quieter risk too: ignoring a conflict can mean someone's update simply disappears. Resolving it keeps each team member's changes, so they can be traced later.

Proper conflict resolution, done promptly, also keeps the commit history clear and consistent. Code is easier to track when every merge in the log makes sense.

How to Resolve Merge Conflicts in Git

Here are four ways to resolve a merge conflict. For Salesforce-specific prevention, see the best practices below.

How to resolve conflicts manually

Git highlights each conflicting section with markers (<<<<<<<, =======, >>>>>>>). From there, resolving it by hand takes seven steps:

  1. Open the conflicted files in a code or text editor when you see the error.
  2. Read through both versions and decide what to keep, remove or merge.
  3. Keep one side's changes, or combine the two.
  4. Edit the code so the logic and syntax still work.
  5. Remove the conflict markers.
  6. Stage the resolved file with git add <file>.
  7. Commit the changes with git commit.

This works best for text-based files you can edit directly. Test as you go, because a hand-merged file can break in ways the markers won't show you.

How to resolve conflicts with a merge tool

A merge tool is often the faster route. Tools like Flosum and VS Code both help, and if you're weighing options for Salesforce, our Flosum vs Gearset comparison puts two of them side by side.

For a wider view, our roundup of Salesforce DevOps tools compares more of them. With a merge tool, the steps are short:

  1. Launch the tool with git mergetool, which can open Meld, KDiff3 or another tool you've configured.
  2. Resolve each conflict in the visual interface by accepting the current branch's changes, the incoming ones or both.
  3. Save your changes.

How to resolve merge conflicts in the Git command line

You don't need anything beyond the Git command line and a text editor to resolve a conflict. The process looks like this:

  • When Git reports a conflict, go to the local repository that contains it. Run git status to list the files the conflict affects.
  • Open the affected file in your text editor.
  • Look for the marker <<<<<<<, which shows where the conflict starts. The changes from HEAD, the base branch, come right after <<<<<<< HEAD.
  • Next comes =======, then the changes from the other branch and the marker >>>>>>> BRANCH-NAME. The example below shows how that looks.
  • Decide which change to keep. You can also combine the two.
  • Delete the markers <<<<<<<, ======= and >>>>>>>, and make the edits you want in the final merge.
  • In our example, we combined both changes.
  • Stage the changes, then commit them with a message that records the resolution.

How to resolve merge conflicts in Git pull request

On GitHub or another Git host, resolving a pull request conflict starts with up-to-date branches. Here's the sequence:

  • Update the code on both branches from the remote first. If either is behind, pull it or push your local changes as needed.
  • Use git checkout to switch to the branch you're trying to merge. To do the same locally, run this command:

git pull origin <the parent branch>

  • The output will look something like this:
  • Open the conflicted file, and you'll see something like this:

Here the line from the master branch, "Hello!", has been replaced by a different print statement in the branch. Your job is to pick the statement from one branch and resolve the conflict.

In this example we'll keep the new branch's statement. Remove the conflict markers, keep the line you want, and the edited file looks something like this:

  • Git now treats your edit as a change. Run git add, and Git fills in a merge commit message when you run git commit.
  • Edit or save that message. Once you do, the PR updates automatically.
  • You're left with a PR with no conflicting edits, ready for review and merge.

Top 5 Reasons for Merge Conflicts in Git

Conflicts have plenty of causes, from parallel editing to deleted files and branches that drift apart. These five come up most often, and each has a fix.

1. Concurrent changes

When several developers edit the same file and their edits overlap, Git can't decide which to keep, so it raises a conflict. Planning your branches, committing often and merging regularly heads most of these off.

2. Overlapping edits

If different branches change the same lines, Git stops instead of guessing, and someone has to review and merge the changes by hand. Prompt code reviews and a bit of coordination prevent most of them.

3. Branch divergence

A long-lived branch keeps collecting changes, and the longer it lives, the harder it is to integrate. Merge or rebase it often so it never drifts too far from the rest.

4. File deletion

Delete a file on one branch while another branch updates it, and Git can't tell what you want. Someone has to decide during the merge whether the file stays or goes.

5. Rebasing issues

Rebasing rewrites commit history, which causes conflicts if others have already pulled those commits. The simplest fix is not to rebase shared branches.

Best Practices to Fix Merge Conflicts in Git

Conflicts never disappear completely, but a few habits keep them under control. These six cut down how often they happen and make the rest easier to fix.

1. Commit changes frequently

Infrequent commits are one of the most common causes of conflicts. Small, frequent commits record changes step by step, so big conflicting changes never get the chance to pile up.

With descriptive messages, each change is clear, and a small change is easier to troubleshoot when it does conflict. Merges get lighter too, so projects finish and deploy sooner.

2. Pull the latest changes regularly

A branch without the latest changes drifts away from everyone else's code, and conflicts follow. Run git pull to sync your local branch with the remote and keep it current.

Pull before you start any new task or feature. Syncing often surfaces conflicts early, long before the final commits.

3. Use feature branch workflow

On a project with several developers, some conflicts are inevitable. A feature branch workflow, where each developer works on an isolated branch, avoids a lot of them.

Once the task is done, merge the feature branch into the main branch through a pull request. The main branch stays clean, direct conflicts during development drop and commits move faster.

In Salesforce, the same workflow applies to metadata once it's under version control for Salesforce. Our guide to Salesforce branching strategies compares the common models.

4. Communicate with the team

Poor communication between people on the same project leads to conflicts, wasted resources and late delivery. Talking to each other has to be part of the process.

Coordinate tasks so two people aren't in the same files at once, and use tools like Jira, Slack and Trello for assignments and progress tracking. When everyone knows which files are changing, overlapping edits get rare.

5. Resolve conflicts early

If a merge conflict occurs, deal with it straight away. Put it off and the codebase gets more tangled, and every later commit is harder to merge.

Regular commits and pulls catch conflicts early, which keeps the project manageable and cuts down on surprise bugs. Clearing conflicts in the development branch also keeps those bugs out of the production org.

6. Avoid large code changes

Big code changes raise the odds of a conflict and make each one harder to untangle. Split large tasks into smaller commits, since small chunks merge more easily.

DORA's analysis of its 2016 and 2017 survey data found that teams achieve higher software delivery and operational performance when they follow a few practices. Two of them are keeping three or fewer active branches and merging into trunk at least once a day.

Flosum's Overwrite Protection to Avoid Conflicts with a Click

Merge Conflict Screen in Flosum

Merge conflicts delay deployments, break builds and stretch timelines. Manual resolution works, but a tool that flags conflicts early saves time on every release.

Flosum's Salesforce DevOps platform does that for Salesforce metadata. Its overwrite protection feature detects merge conflicts and lets developers review and resolve them with a click.

Before a deployment, Impact Analysis flags components that are new or different, and developers review the differences to decide whether a merge is needed. Fewer bugs reach the production org as a result, and the Salesforce code stays clean.

Automated pull requests and Impact Analysis catch conflicts sooner, too. Teams resolve them throughout development and find bugs and overwrites before they get into production.

FAQs

What is a Git merge conflict?

A Git merge conflict happens when two branches edit the same part of a file and Git can't pick a winner on its own. Git pauses the merge, marks the clashing lines and waits for you to choose before you commit.

How do you resolve merge conflicts in Git?

Open each conflicted file, keep one side or combine both changes, then delete the conflict markers, stage the file with git add and commit the merge. Merge tools such as git mergetool, VS Code or Flosum show the conflicts side by side and speed this up.

How do I resolve merge conflicts in Git pull requests?

Update both branches from the remote with git pull, then switch to the branch you are merging. After you edit the conflicted files and strip out the conflict markers, stage them with git add and commit, and the pull request updates automatically.

What is the Git tool to resolve merge conflicts?

Git ships with git mergetool, which opens each conflict in the visual merge tool you configure. VS Code and Meld are popular choices, and Salesforce teams can use a DevOps tool like Flosum to review metadata conflicts.

How do you avoid Git merge conflicts?

Pull often, keep branches short-lived and commit small changes frequently. Agree on a branching strategy so two people rarely edit the same lines at once.

Request a demo to see how Flosum flags metadata conflicts before they reach production.

Table Of Contents
Author
Stay Up-to-Date
Get flosum.com news in your inbox.

Thank you for subscribing

Ship Salesforce changes faster
Teams on Flosum report 40% higher development velocity and 75%
fewer merge conflicts.
Free to start  |  No credit card required