DEV Community

harmeed
harmeed

Posted on

HOW TO RESOLVE MERGE GIT CONFLICT.

What Are Merge Conflicts and What Is Git Merge?
Git is a version control program that maintains a history of every version of your files. Anytime you want, you can recover an earlier version by going back to any of the versions.

Consider that you have produced a file named “doc.txt” and uploaded it to a Git repository. The file is currently associated with its most recent version. The file now has a new version attached if your coworker edited it and sent it back to the repository.
You can maintain consistency between the present content of the file and earlier versions by using the Git Merge functionality. This is crucial because everyone should always be editing the most recent content of the file without overwriting modifications made to earlier versions.

Before pushing a new update to the same file, Git merge enables you to combine changes made by other developers.
We should be cautious of two things when using Git merge:

What kinds of modifications were made between two versions of a file? Existing content may be changed, deleted, or replaced with new content.

There are two different scenarios that are possible. Either the same region of the file saw modifications, or the changes occurred in distinct sections of the file. Same region refers to modifications that have been done to a file’s vicinity (for example, the same paragraph, line, etc.).

Fortunately, Git uses the auto-merge approach to handle the majority of these situations automatically. Git won’t, however, attempt an auto-merge if the same area of the file has undergone changes. Instead, it defers to your decision to resolve.

How Can Merge Conflicts Be Solved in Git?
The procedures required to resolve merge conflicts in Git could be shortened by taking a few specific actions.

Opening the conflicted file and making the appropriate adjustments is the simplest way to remedy the issue.

After making changes to the file, we may stage the newly merged material using the git add a command.

The git commit command is used to create a new commit as the last step.

A fresh merge will be created by Git. Decide to complete the integration.

Now let’s examine the Git commands that can be crucial in settling conflicts.

Git Commands to Resolve Conflicts

  1. git log — merge
    The list of commits that are creating the conflict is produced by the git log — merge command.

  2. git diff
    To find discrepancies across states of repositories or files, use the git diff program.

  3. git checkout
    The git checkout command is used to reverse file modifications or to switch branches.

  4. git reset — mixed
    To reverse changes made to the working directory and staging area, use the git reset — mixed command.

  5. git merge — abort
    The git merge — abort command facilitates stopping the merging operation and returning to the initial state.

  6. git reset
    When merging, the git reset command is used.

Top comments (0)