How to resolve git conflicts in Magento 2 Module

Git is a very powerful tool for developers. It is a free and open-source tool used for source code management.

  • It is a version control system to manage small to large project
  • It is used to track changes in source code.
  • It provides an environment to enables multiple developers to work in parallel with each other.

Basic Git Commands

  • git clone
  • git status
  • git commit
  • git add
  • git push
  • git pull
  • git init
  • git config

What is Git conflict?

A git conflict arises when changes have been made on file same line from two separate branches. Git is able to merge the changes automatically only if the commits are on different lines or branches. It can be due to two reasons:

Here is an example:

Assume two developers John and Sara are working on a project and they take/pull the same code from the remote repository. They made changes to the same file and John has pushed his code to the remote repository Now Sara trying to push his code It will not work. Git will not allow pushing the code as the file is already changed by John to the remote repository.

To prevent such conflicts, developers must work in separate branches. The Git merge command combines separate branches and resolves any conflicts.

Searching for an experienced
Magento Company ?
Read More


Now, John and Sara created their respective branches and made changes to the same file John pushed the code to the remote branches, and after that, Sara also pushes the code to his branch the Git merge command combines separate branches and resolves any conflicting edits if it not made to the same line. But if the changes are on the same line then it will create a merge conflict

Type of Git Merge Conflicts

There are two states when a merge conflict takes place:

  • Starting the Merge: Merge will not start if there are any changes in the working directory. It can be resolved by staging your changes using git commands.
  • During the Merge: It can be due to the conflict between the local branch and the remote branch. Git resolves the possible conflicts rest but if any changes on the same file and same line then you have to resolve your git conflicts manually.
git-conflict-on-push

How to Resolve Git Conflicts

1. Pull and merge the latest changes from the remote repository

git pull

If possible git will auto-resolve the conflicts. Skip 2

git-pull

Else. You will see: Automatic merge failed; fix conflicts and then commit the result.

git-pull-merge-failed

2. Open the conflict file mentioned. You will see something like:

<<<<<<< HEAD
- Commit with Message In details
=======
- Commit with Message Updated
>>>>>>> 2616df3fc3b10b677c0926fcadc8594f2f5bb1b6

Explanation:

<<<<<<< HEAD
- Commit with Message In details // Where the conflict at working directory
=======
- Commit with Message Updated // Changes from remote
>>>>>>> 2616df3fc3b10b677c0926fcadc8594f2f5bb1b6 // Commit Id

3. Resolve the conflict. The conflicting changes are marked by <<<<<<< HEAD and >>>>>>>. You need to choose which changes to keep and which to discard.

Manually edit the file to combine the conflicting changes.

- Clone
- Pull
- Status
- Add
- Commit with Message In details
- Push

Or VSCode also provides support to resolve git conflict:

Resolve-conflict-using-VScode
// Accept Current change - will merge the current changes and merge
- Clone
- Pull
- Status
- Add
- Commit with Message In details
- Push

// Accept Incoming Change - will merge the changes from remote
- Clone
- Pull
- Status
- Add
- Commit with Message Updated
- Push

// Accept Both Change - will merge both local and remote changes
- Clone
- Pull
- Status
- Add
- Commit with Message In details
- Commit with Message Updated
- Push

4. Save the changes and add the changes to git:

git add .

5. Commit the changes

git commit -m "resolved conflict"

6. Push the changes

git push origin master
merge-the-conflict

We resolve the git conflicts. Hope It helps 🙂

Learn more from Webkul blogs here:


Source link