Resources /
Blog

How to Resolve Merge Conflicts in Git?

4
Min Read

Merge conflicts can make it harder for Salesforce development teams to deliver projects faster and more efficiently. They can also make it difficult for teams to stick to their timelines, considering resolving merge conflicts in Git requires additional effort and resources. As these issues can affect the overall quality of the Salesforce code and project, addressing merge conflicts in Git is vital for any organization using Salesforce.

In this article, we've addressed all the important questions about resolving merge conflicts in Git and best practices to resolve them.  Let's get started.

What Is a Merge Conflict?

A merge conflict is an event where Git cannot merge changes from different branches. This occurs when Git can't merge updates automatically due to overlapping changes in the same file. As Git can't decide which change to keep, it pauses the merge process, leading to merge conflicts.

Merge Conflict in Salesforce

When there is a conflict in merges, Git highlights conflicting sections and makes it easy to spot issues. The developers then need to make the changes manually, which can lead to longer deployment times and inefficiency.

There are two types of merge conflicts in Git: 

  1. Conflict at the start: 

Merge conflicts at the start of the merge happen when there are unfinished changes in a project's directory or staging environment. Here is the error message that you will get for this type of merge conflict:

“error: Entry '<fileName>' not uptodate. Cannot merge. (Changes in working directory)."

  1. Conflict during the merge: 

This merge conflict happens when the branches are merged and the current local branches are in conflict. It shows a conflict with the code developed by the current developer. Here is the error message that you will get for this type of merge conflict:

"error: Entry '<fileName>' would be overwritten by merge. Cannot merge. (Changes in staging area)."

Why Resolve Merge Conflicts?

Unresolved merge conflicts often do more harm than good. Addressing merge conflicts in Git is vital for a variety of reasons, such as:

  • Clean, conflict-free code is essential for smooth deployments and continuous integration (CI/CD) processes.
  • In team projects, unresolved conflicts can block development and affect the project's progress. Fixing these issues ensures smooth collaboration and continuous improvement.
  • Unresolved conflicts can break the code and lead to build failures and bugs. Reviewing and addressing conflicts helps keep your project stable.
  • Ignoring disputes may lead to losing essential updates. Resolving conflicts preserves valuable team member changes and can be tracked back.
  • Timely and proper conflict resolution helps maintain a clear and consistent commitment history. It can make code-tracking easier and more organized.

How to Avoid Merge Conflicts in Git?

Avoiding merge conflicts in Git is vital for various reasons, such as improving code efficiency, faster development, quicker deployment, and more. Therefore, it becomes important to understand how you can avoid merge conflicts in Git from occurring, saving you plenty of time and hassle. Here are a few common methods to prevent merge conflicts in Git.

How to avoid the conflicts manually

When a merge conflict occurs, Git highlights conflicting sections using markers (<<<<<<<, =======, >>>>>>>).

Here is what you can do to resolve the conflict:

  1. Open the conflicted files in a code text editor as you get the error
  2. Review conflicting changes and decide what to keep, remove, or merge
  3. You can either remove one of the files or combine both codes.
  4. Edit the code to resolve the issue, ensuring correct logic and syntax
  5. Once done, remove the conflict markers
  6. Use Git add <file> to stage the resolved file.
  7. Now, commit the changes with git commit

This method works well for text-based files where manual edits are possible. Test the code thoroughly while resolving issues manually to prevent unexpected problems in the project.

How to avoid using a merge tool

Using a tool to address merge conflicts in Git is another effective and faster way. Tools like Flosum, VSCode, etc., to resolve merge conflicts faster.

Here is what you can do to resolve the issue:

  1. Launch the tool with git mergetool
  2. Resolve conflicts through the visual interface.
  3. Save changes

How to resolve merge conflicts in the Git command line

You can use the Git command line and your preferred text editor to resolve merge conflicts in Git.

Here are the steps to do it:

  1. Once Git notifies you about a merge conflict, navigate to the local Git repository containing the conflict. Now, create a list of files that may be affected due to the merge conflict.
  2. After identifying the file, open it in your chosen text editor.
  3. You can identify the start of the conflict with the help of the conflict marker <<<<<<<. As you open the conflicted files in the text editor, you will see the changes from the Head or base branch starting after the marker <<<<<<< HEAD.
  4. You can see ======= and the changes you made, followed by the changes in the other branch accompanied by >>>>>>> BRANCH-NAME.

Here is an example of how that looks:

  1. Decide the change you want to keep in the file. You can also combine both the changes in the file for resolving conflicts.
  2. Delete the conflict markers <<<<<<<, =======, >>>>>>> from the file and make the changes you want in the final merge.
  3. For the example we considered,  we combined both changes to resolve conflicts.
  1. You can add or stage the changes you made. Once done, you can commit the changes with a comment to mark the resolution.

How to resolve merge conflicts in Git pull request

Follow the steps below to resolve merge conflicts in the Git pull request:

  1. Before working on resolving merge conflicts, make sure to update the code on both branches with the remote branch. If that's not the case, you must pull both branches or push any local changes as needed.
  2. Use the git checkout command and switch to the branch you are trying to merge. You can do the same locally with the following command: 

git pull <the parent branch> origin

  1. Now, you will get an output that might look similar to the one below:
  1. Upon opening the conflicted file, you will see something as given below:

This tells you that the command line from the master branch, "Hello!" is replaced by a different print statement in the branch. Your role is to choose the statement from one branch and resolve the issue.

For this example, let's go with the new branch's statement. Remove the conflict markers from the file and keep the statement you want to retail. After editing, it will look something like this:

  1. The edit you have done now is a "change" in Git. Hence, you need to do a Git add to autogenerate a commit message that will be autogenerated when you do a Git commit.
  2. This will allow you to modify or save the commit message. Upon finishing it, the pr you created will be updated automatically.
  3. And now, you have a PR ready to be reviewed and merged with no conflicting edits.

Top 5 Reasons for Merge Conflicts in Git

Merge conflicts in Git can occur due to numerous reasons. It can range from parallel editing to file deletion and branch divergence, but there's more to know. Let's explore each of these issues in detail here.

1. Concurrent changes

When multiple developers work on the same file, Git struggles if edits overlap. It can't decide which changes to keep, leading to conflicts. One of the best ways to address this issue is to plan proper branching, frequent commits, and regular merges.

2. Overlapping edits

If different branches modify the same code lines, Git can't merge them automatically. Developers must review and manually merge changes. You can avoid this by timely code reviews and team coordination.

3. Branch divergence

Long-lived branches accumulate many changes and make integration harder. Merging these branches increases conflict risks. Hence, keep an eye out for long-lived branches and merge or rebase them to minimize divergence.

4. File deletion

If one branch deletes a file while another branch updates it, Git gets confused about what to keep. Developers must decide whether to keep or remove the file during the merge.

5. Rebasing Issues

Rebasing rewrites commit history and creates conflicts if others have already pulled those commits. The best way to resolve such scenarios is to avoid rebasing shared branches.

Best Practices to Fix Merge Conflicts in Git

Addressing merge conflicts in Git can be done in a range of ways. Let's explore the best ways to resolve Git merge conflicts here.

1. Commit changes frequently

A chief reason for merge conflicts is the lack of frequent commit changes. Hence, you must make small, frequent commits to ensure code changes are tracked incrementally. This reduces the likelihood of large, conflicting changes accumulating.

You can also use descriptive commit messages to clarify changes. Frequent commits also make troubleshooting easier when resolving conflicts since the changes are more specific and easier to understand.

Frequent commits reduce the load for merging, leading to faster project completion and deployment.

2. Pull the latest changes regularly

Keeping the latest changes for a long time in the pipeline can also lead to merge conflicts as customizations made are not implemented. Avoid this scenario by syncing your local branch with the latest remote changes using git pull. This keeps your branch updated and minimizes code divergence. Make it a habit to pull before starting a new task or feature.

Furthermore, regular syncing ensures potential conflicts are detected early and reduces merge issues during final commits.

3. Use feature branch workflow

When multiple developers work on a project, it is quite natural for merge conflicts to happen. Adopting a feature branch workflow where each developer works on an isolated branch is the best strategy to avoid this.

After completing the task, merge the feature branch into the main branch through a pull request. This keeps the main branch clean and reduces the risk of direct conflicts during development. It can also lead to faster commits of changes, leading to efficient project progress and deployment.

4. Communicate with the team

Lack of proper communication between teams working on the same project can lead to merge conflicts, wasted resources, and delayed project delivery. Therefore, communication between the teams working on the project should be prioritized.

You can do the same by coordinating tasks within the team to avoid working on the same files simultaneously. You can also use project management tools like Jira, Slack, and Trello for task assignments and to track the project's progress.

Clear communication about ongoing work ensures developers know which files are being modified, preventing overlapping changes.

5. Resolve conflicts early

Address merge conflicts as soon as they arise. Delaying conflict resolution can lead to a more complex codebase and increased difficulty merging commits.

Early conflict detection through regular commits and pulls keeps the project manageable and less prone to unexpected bugs. It will also help fix conflicts in Git faster.

Avoiding all the conflicts early in the development branch can also help you avoid bugs from getting into the production org.

6. Avoid large code changes

Making large code changes can also lead to multiple conflicts in Git. It increases the chances of conflicts and makes addressing them even harder.

Avoid this by splitting large tasks into smaller, manageable commits. Working on smaller chunks of code makes the changes minimal and easier to merge.

Avoiding extensive changes in a single commit reduces the risk of conflicts. This also leads to smoother and more efficient integration.

Flosum's Overwrite Protection to Avoid Conflicts with a Click

Merge Conflict Screen in Flosum

Merge conflicts in Git can lead to diverse issues, from delayed deployments and inefficient codes to prolonged project timelines. Resolving merge conflicts is vital to ensure your projects stick to the timetable and deliver as needed. While manual resolving can help, using a tool to streamline resolving merge conflicts is critical. This is where Flosum, a Salesforce native DevOps platform, can help.

With its overwrite protection feature, developers can review and resolve merge conflicts with a click. It automatically detects and allows the developers to review where merge conflicts happen. Flosum offers an Impact Analysis feature that detects the new and different components. Developers then can review the differences between the components and decide if merging is needed.

Using Flosum for merge conflicts brings diverse advantages:

  • Flosum limits the chances of bugs entering the production org and keeps Salesforce code top-notch.
  • Flosum's automated pull request and Impact Analysis helps avoid merge conflicts faster.
  • Resolve merge conflicts across the development process and find bugs and overwrites before they get into the production org.

If you're looking for an efficient tool that can transform your DevOps processes and help you deliver faster projects in Salesforce, Flosum can help.

Request a demo to see Flosum in action now.

Frequently Asked Questions

How do you resolve merge conflicts in Git?

There are several ways to resolve merge conflicts in Git. You can avoid the conflict manually by manually reviewing the conflict markers and making the required changes. Either merge the codes that conflict or remove them entirely to resolve git merge conflicts. Another way to avoid merge conflicts is to use a tool like Flosum. It offers an interface with all the conflicts and the help you need to address the conflicts. Using tools can save time and effort.

How do I resolve merge conflicts in Git pull requests?

It is pretty simple to resolve merge conflicts in Git pull requests. You can start by updating both branches with the remote branch using git pull. Switch to the branch you are merging and resolve conflicts by editing the files. Then, remove the conflict markers and save the changes. Stage the resolved files with Git Add and commit them to have your pull request update automatically.

What is the Git tool to resolve merge conflicts?

Git offers diverse tools to resolve Git merge conflicts. One of the best is Git's own git mergetool, which comes in-built. Another tool to use is VS Code or Meld. You can also use a Salesforce-native tool like Flosum to address as many conflicts as needed on Git. 

Table Of Contents
Author
Stay Up-to-Date
Get flosum.com news in your inbox.
Read about our privacy policy.
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.