How to Resolve Merge Conflicts in Git?
Overview
A merge conflict is an issue that arises when we try to merge two branches that have been edited at the same time and in the same file, then Git will not be able to merge them as Git cannot identify the version that needs to be used for changes.
A merge conflict can happen at two separate points first when the merging process starts and second during the merging process. The very first step that we perform during the merge conflict resolution is to understand what is causing the merge conflict, for checking more details, we can use the git status command.
Pre-requisites
The prerequisites for learning about how to resolve merge conflicts in Git can be a basic understanding of Version Control Systems, Branching, and Git. Let us discuss them briefly before learning about the git merge conflict and how to resolve merge conflicts in Git.
Version Control Systems
A version control system is a tool in software development that tracks the changes in the code, documents, and other important information regarding a certain code base (or project), etc.
There are two types of Version Control systems:
- Centralized Version Control Systems (CVCS)
- Distributed Version Control Systems (DVCS)
Git
Git is a version control system that tracks the changes in the code, documents, and other important information regarding a certain code base (or project), etc. Git is free and one of the most widely used version control systems. We can use Git through the command line as well as through its graphical user interface (GUI). The command line or terminal version of Git is known as Git Bash on the other hand the GUI version of Git is known as Git GUI. Git tracks the changes in a project and saves a certain state that is known as commit. A commit is a snapshot of the file's current version(s). So, we track these commits and can revert to a certain commit if we want.
Branching
A branch is an independent line of development that is used to add certain features and fix bugs without hampering the main project. So, we can develop new features in parallel and when the development is completed, we can add the back to the main project. By default, all the GitHub repository has the master branch which can be used for production.
So, a new branch is a copy of the master branch which is created for bug fixes and for the addition of new features. After the bug is fixed or new features are added, we can merge the branch to the master branch. The git branch command enables us to perform parallel development. The command can create, rename, list, and delete branches.
GitHub
GitHub is a cloud-based central repository that can be used to host our code for team collaboration. It is a hosting service that is used to manage the git repository in the central server. GitHub is a free (for certain limits) and easy-to-use platform that enables teammates to work together on projects. GitHub tracks the changes made in the project workflow and we can also revert ba a certain state of the project (as GitHub saves the history of the project). GitHub supports open-source development where several developers can collaborate and support each other in the development process.
Anyone can create an account on the GitHub platform to host their code, files, and documents. GitHub sells hosted private code repositories, and other collaborative business model plans to make money as an organization.
What is a Git Merge Conflict?
A merge conflict is an issue that arises when we try to merge two branches that have been edited at the same time and in the same file (refer to the image provided below for more clarity), then Git will not be able to merge them as Git cannot identify the version that needs to be used for changes.
In case of a merge conflict, Git stops the merging process just before performing the merge conflict so that the user can resolve the conflict manually. Refer to the image provided below for more clarity.
We use the git merge command and git rebase command for merging multiple commits. The git merge command only makes changes to the target branch, the source remains the same as the git merge command saves the history of the branch. If we use the git rebase command, all the changes are compressed into a single stream called patch. Now, if the team is small or there is a single developer then we should the git rebase command. On the other hand, if we have a comparatively larger team then we should use the git merge command. We will be learning more about the commands for resolving the merge conflict in the later sections.
Types of Merge Conflicts
A merge conflict can happen at two separate points first when the merging process starts and second during the merging process. Let us now learn about the various types of merge conflicts that can occur in Git.
1. Merge Conflict that Starts at the Time of the Merge Process
There may arise a situation when the merge process will not start i.e. when we have changed the working directory staging area of our current project. Since Git will not be able to choose the correct staging area hence the merging process won't even start.
So to avoid such kind of early merge conflicts, we need to first stabilize the pending changes using various git commands. After the staging area is stabilized properly then we can start the merging process.
We can use a command like git stash, git checkout, git commit, or git reset. If we have a merge failure at the start of the merging process, the following error message will be shown as output:
2. Merge Conflict that Takes Place During the Merge Process
Another type of merge conflict that usually happens during the development process is when two branches have been edited at the same time and in the same file, so Git gets confused and shows us a merge conflict. The in-process git conflict also resembles that there must be a conflict of merging between the currently merging branch and the local branch.
Git first tries to resolve the conflict as much as on its end but if there are things that need to be handled manually is shown as a Git conflict. If we have a merge failure during the merging process, the following error message will be shown as output:
Creating a Merge Conflict
To understand how to resolve merge conflicts in git, let us first create a git conflict and then try to resolve it (in the later section). Suppose we have a file named test.txt. We have already created the file, it has some content and we have committed the file using the git add and git commit -m commands.
Now suppose we have created another branch named test-branch and then added some content that overrides the original test.txt. once we are done, we again added the file in the staging area and then committed the change (as we have done previously) by checking out the main branch. Now as we can see we have two new commits which create the merge conflict.
So, if we try to merge the test branch, we will be shown the following output as a conflict:
We will now learn how we can resolve the conflicting issue in the later section.
How to Identify Merge Conflicts?
Let us now learn how we can identify a merge conflict in Git. As we know that if there is some sort of conflict in the merging process then Git will provide us a description in the output and lets us know the entire conflict that has occurred. If we want any further insight or if we want to know further, we can use the git status command for the same.
A sample output that arises when we run the git status command is:
The above output depicts that the test.txt file has been found in a modified state and there are some unmerged paths due to a conflict in the depicted file. We can open the file using Linux's cat file-name command (as cat test.txt) to see the content that has been modified and needs to be changed.
A sample output of the cat file-name command on the conflicting file can be:
We have used the cat command so that we can easily visualize the problem-causing area. Git used three symbols to show the problem area, these symbols are altogether known as conflict dividers:
- <<<<<< HEAD
- ========
- >>>>>> test-branch.
The ======== symbol depicts the center of the conflict so all the content between the <<<<<< HEAD and ======= is the content that is causing conflict. Apart from this the content between the ======= and >>>>>> test-branch is the content that is already present in our merging branch.
Git Commands to Resolve Merge Conflicts
So far we have discussed a lot about the merge conflicts in git. Let us now learn how to resolve merge conflicts in git using the various git commands. One of the most basic ways to resolve a merge conflict is to edit or modify the conflicting file.
We first need to open the conflict file in any text editor of our choice and then simply remove all the content that lies in the conflict dividers. Once we are done with the editing then we can add the file to the staging area using the git add command and then we can finalize the new commit using the git commit -m command. The overall command for the same is:
Before merging the files, we must perform these three steps:
Now to perform the merging, some preparation steps need to be followed. Let us discuss them briefly getting into the essential commands of git merging.
-
Confirm the receiving branch:
We will first check whether the HEAD of the branch is pointing to the correct merging branch or not. The checking can be performed using the git status command that is used to check the status of the repository. If we need to change the branch then we can use the git checkout command to move to a certain branch. -
Fetch the latest remote commits:
Before merging, we need to first check if our receiving branch is up-to-date with the latest changes in the remote repository. For fetching the latest changes (to update the current branch), we can use the git fetch command. After fetching, we will use the git pull command to ensure that our master branch has the latest updates. -
Merge:
After performing the above steps, we can now initiate the merging by using the git merge command.The various commands that are used in git merging.
The basic git merging command:
-
The command to merge a certain commit of the active branch.
-
The command to merge a certain branch.
Let us now look at the various commands that are used to resolve the git conflicts.
-
git log --merge:
If we want to produce the list of all the git commits that cause the git conflict in the current branch then we can use the git log --merge command. -
git diff:
If we want to see the difference between the states files or repositories in the current branch then we can use the git diff command. -
git checkout:
If we want to undo the changes that were made to the files then we can use the git checkout command, we can also use this command to change the branch. -
git reset --mixed:
If we want to undo the changes that were made to the staging area and the working directory then we can use the git reset --mixed command. -
git merge --abort:
If we want to abort the current merging or exit then we can use the git merge --abort command. This command sets the state of the branch to its previous state as well (the state before the merging process was started). -
git reset:
The git reset command is used to reset the changes made in the working tree of a repository. The git reset command changes the indexing as well as the working tree.The git reset command deals with the index and working tree and resets the head pointer to the previously made commit. There are three forms of the git reset command and depending on the type of the command the resetting is made.
Resolving Git Merge Conflicts
The very first step that we perform during the merge conflict resolution is to understand what is causing the merge conflict, for checking more details, we can use the git status command.
Now, when we open the conflicting file(s), we can see the problematic area (that is causing conflict) enclosed in a "<<<<<<< HEAD" and ">>>>>>> branch-name".
So, first, we need to clean up the lines that are causing conflict. For doing so, we can contact our fellow developers and team as it is a crucial matter. Finally, we can save the new state of the file. And at last, we can merge the files with no conflict.
We can also use GitHub to merge a conflict. Please refer to the article, using github to review a pull request and selective merging for more clarity.
Conclusion
- Git is a version control system that tracks the changes in the code, documents, and other important information regarding a certain code base, etc.
- A merge conflict is an issue that arises when we try to merge two branches that have been edited at the same time and in the same file, then Git will not be able to merge them as Git cannot identify the version that needs to be used for changes.
- A merge conflict can happen at two separate points first when the merging process starts and second during the merging process.
- The very first step that we perform during the merge conflict resolution is to understand what is causing the merge conflict, for checking more details, we can use the git status command.
- If there is some sort of conflict in the merging process then Git will provide us a description in the output and lets us know the entire conflict that has occurred. If we want any further insight or if we want to know further, we can use the git status command for the same.
- The ======== symbol depicts the center of the conflict so all the content between the <<<<<< HEAD and ======= is the content that is causing conflict. Apart from this the content between the ======= and >>>>>> test-branch is the content that is already present in our merging branch.
- If we want to produce the list of all the git commits that cause the git conflict in the current branch then we can use the git log --merge command.
- If we want to see the difference between the states files or repositories in the current branch then we can use the git diff command.
- If we want to undo the changes that were made to the staging area and the working directory then we can use the git reset --mixed command.
- The git reset command is used to reset the changes made in the working tree of a repository. The git reset command changes the indexing as well as the working tree. If we want to undo the changes that were made to the files then we can use the git checkout command.