Git merge conflicts occur when two or more branches that have diverged from a common ancestor are merged and contain conflicting changes to the same file(s). To resolve merge conflicts in Git, follow the steps below:
- Before merging the branches, make sure you have the latest version of the target branch checked out by running:
git checkout target-branch
git pull
This ensures that you have the most up-to-date version of the branch and reduces the chance of conflicts.
- Merge the source branch into the target branch using the following command:
git merge source-branch
This will attempt to merge the changes from the source branch into the target branch. If there are conflicts, Git will display an error message and stop the merge process.
- To resolve the conflicts, open the file(s) that have conflicts in a text editor and look for the conflict markers. The conflict markers are strings that Git uses to indicate the conflicting changes in the file. An example of a conflict marker is:
<<<<<<< HEAD
// changes made to the target branch
=======
// changes made to the source branch
>>>>>>> source-branch
- Edit the conflicting lines to remove the conflict markers and keep the changes that you want to keep. For example:
// changes made to the target branch
// changes made to the source branch
- Save the file and add it to the staging area using the command:
git add file.txt
- Continue the merge process by running:
git merge --continue
This will attempt to merge the next set of changes. If there are additional conflicts, repeat steps 3-6 until all conflicts have been resolved.
- Once all conflicts have been resolved, commit the changes using the command:
git commit -m "Merge source-branch into target-branch"
This will create a new commit that includes the changes from the source branch merged into the target branch.
Example:
Suppose we have two branches feature
and main
, and we want to merge the changes from the feature
branch into the main
branch. If there are conflicting changes to the file.txt
file, the merge process might look like this:
$ git checkout main
$ git pull
$ git merge feature
Auto-merging file.txt
CONFLICT (content): Merge conflict in file.txt
Automatic merge failed; fix conflicts and then commit the result.
At this point, Git has detected a conflict in the file.txt
file and stopped the merge process. To resolve the conflict, we open the file.txt
file in a text editor and look for the conflict markers:
<<<<<<< HEAD
// changes made to the main branch
=======
// changes made to the feature branch
>>>>>>> feature
Here, Git is indicating that there are conflicting changes to the file.txt
file. The lines between <<<<<<< HEAD
and =======
are the changes made to the main
branch, and the lines between =======
and >>>>>>> feature
are the changes made to the feature
branch.
We edit the file to resolve the conflict, remove the conflict markers and keep the changes we want:
// changes made to the main branch
// more changes made to the feature branch
We save the file and add it to the staging area using the command:
$ git add file.txt
We continue the merge process by running:
$ git merge --continue
If there are additional conflicts, we repeat steps 3-6 until all conflicts have been resolved. Once all conflicts have been resolved, we commit the changes using the command: