Managing Conflict

How to Manage Conflicts in Git

If you work with another developer on the same project and edit the same file on different branches simultaneously, both of you may change the same line in the same file. In this case, it is hard to judge which one becomes the master code. This situation is called conflict.

Another case of conflict is the case where one developer deletes a file while another developer continued working on the file.

If you try to merge the branches with a conflict, the Git system won't be able to complete the merge operation automatically. In that case, the Git system creates an alert identifying the location of the conflict through the registered text editor.

The flow chart below illustrates the typical workflow when you run the git merge command.

Git Managing Conflict Flowchart
  1. Run the git merge command to trigger the merge process.
  2. When there is no conflict, the Git system automatically completes the merge process.
  3. When there is a conflict, the Git system suspends the automatic merge process. At this point, the merge process is still running manually; the edited files are staged during this process so that the files are ready to be committed.
  4. If you want to stop the merge operation, run the git merge --abort command. When you run the command, the merge operation is aborted and the status of working files and branches will be reverted to the status they had before running the git merge command
  5. If you want to resolve the conflict and continue the merge operation, edit the conflicting lines of code to resolve the conflict.

Options to solve a conflict

To resolve the conflict, the Git system suggests four options through the registered text editor like shown in the image below. If you click one of the first three options, the changes are reflected in the text editor. If you click the last "Compare Changes" option, a read-mode text editor launches to show the comparison of the two branches.

  1. Accept Current Change (Keep the changes on the branch where you are executing the merge command)
  2. Accept Incoming Change (Keep the changes on the branch being merged)
  3. Accept