Git Rerere: Automating Conflict Resolution
Merging two branches in Git is the most common operation that users do to get the project’s expected outcomes. It can be done via the “git merge” command. But what if both branches have the same file names and different contexts? In that specific scenario, Git confuses, while merging these files, and shows you the conflict that says “fix conflicts and then commit the results”. To avoid this conflict, the Git rerere tool is brought into action.
The expected outcomes from this article are:
How to Automate Conflict Resolution Using Git Rerere?
For an in-depth understanding of conflict resolution, let’s build a situation in Git where two files are created with different contexts but the same name. Then, we will try to merge these files and try to resolve the particular conflict. So, stay tuned with us, walk through the following steps, and get the answer to your query.
Step 1: Go to Git Repository
Open Git bash and jump to the Git repository using the “cd” command:
In our case, we have been moved to the “git-rerere” directory.
Step 2: Create File
Right now, we are in the “master” branch. Let’s create a new file named “file.txt” and write some content using the “echo” command:
The file “file.txt” has been created with the above-given message in the command.
Step 3: Track File
To bring this file to the tracking area, use the “git add” command:
The file “file.txt” has been tracked.
Step 4: Commit Changes
After that, commit changes in the repository by using the “git commit” command:
Changes have been committed successfully.
Step 5: Switch Branch
Now, let’s switch from the existing branch to the new one. For instance, we are switching to the “topic” branch. To do so, run this command:
The branch has been created and switched from “master” to “topic”.
Step 6: List Content
Use the “ls” command to list down the content of the directory:
The same file “file.txt” is available in the repository.
Step 7: Modify File Content
Let’s modify the content of the file with the new message using the “echo” command and redirection operator:
Step 8: Track File
Track the modified file using the command “git add”:
The modified file has been tracked.
Step 9: Commit Modified Changes
Commit the modified changes with the help of the “git commit” command as provided below:
The changes in the modified files are committed.
Step 10: Switch to the Previous Branch
Move back to your previous branch using the below-mentioned command. In our scenario, it was “master”:
Step 11: Merge Both Branches
Let’s merge the content of the “master” branch with the “topic” branch using the “git merge” command:
Upon executing the above command, you can see that Git has displayed the conflict. For the time being, abort the merging mode and enable the Git rerere.
How to Enable Git Rerere to Automate the Conflict?
To enable the Git rerere tool in Git, look quickly at the 2-step guide.
Step 1: Automate Conflict
To automate the conflict, enable the Git rerere tool with the help of the given command:
By executing the above command, the Git rerere has been enabled.
Step 2: Merge Again
After enabling the Git rerere, let’s try to merge both branches again:
From the above output, you can see that the merging has been without any conflict.
Conclusion
Git rerere is the tool in Git that is utilized to automate conflict resolution by enabling it. To enable this, use the “git config –global rerere.enabled true” command. In the above mentioned, we have seen in detail that resolution conflict occurs when merging two branches with the same file name but in different contexts; that can be resolved by enabling the Git rerere.
Source: linuxhint.com