Git Merge conflict
Every project evolves changes in files. Each change refers to different versions of the project. With Version Control System (VCS), you can save these versions like in a database. Git is a most popular version control system. Git helps to understand which files were modified and who did it. It also helps to restore to any previous versions of the project in case if anything goes wrong.
What is a Git Merge Conflict?
merge conflict is an event that takes place when Git is unable to resolve differences in code between two commits automatically. Sometimes multiple developers may try to edit the same content. If Developer A tries to edit the same code that Developer B is editing a conflict may occur. If two people change the same line in the same file ie if one person decided to delete it and another person decides to modify it, git doesn’t know which is correct. It will be marked as a conflict. Git marks the problematic area in the file by enclosing it in ankle brackets.
The following example simulates a merge conflict and let us examine and resolve it.
In the master branch,
- Create a new directory named “mergeconflict “, change to that directory, and initialize it as a new Git repo.
- Create a new text file “merge.txt” with some content in it.
- Add merge.txt to the repo and commit it
Next, we will create a new branch to use as the conflicting merge.
- Create and checkout to a new branch named “newbranch“
- overwrite the content in merge.txt.
- commit the new content
- Switch to master branch.
- Append content to merge.txt.
- Add merge.txt to the repo and commit it.
- At this time we’ll merge the branch ‘master’ and branch ‘newbranch’
- A conflict appears.
How to identify merge conflicts?
- Git produces some descriptive output letting us know that a CONFLICT has occurred. We can know the status by running the git status command.
- The output from git status indicates that there are unmerged paths due to a conflict. The merge.txt file now appears in a modified state.
- Let us examine the file using the cat command to show the contents of the merge.txt file and see what is modified.
- The “<<<<<<<” characters denote the current branch’s edits (in this case, “HEAD,” which is another word for the current branch(master)). The ======= line is the “center” of the conflict. All content between the center and >>>>>>> newbranch is content that is present in our merging branch (newbranch).
How to resolve merge conflicts using the command line?
- The developer decides what stays and what should be removed.
- Open the merge.txt file in an editor. For our example let us remove all the conflict dividers. The modified merge.txt content should then look like this:
- Once the file has been edited, use git add merge.txt to stage the new merged content. To finalize the merge create a new commit.
- Git will see that the conflict has been resolved and create a new merge commit to finalize the merge.
DevOps Gurukul is providing the Best online Linux training in Kerala. Our aim is to support the right candidates and provide the Complete RedHat Linux Training Course.