Resolve merge conflict on Gerrit
Do not miss this exclusive book on Binary Tree Problems. Get it now for free.
In this article, we have explained the steps to resolve merge conflict on Gerrit with the detailed step by step commands. This can be solved in the console using rebase command and manually, fixing the conflicts in code.
Pre-requisite:
Fix merge conflict on Gerrit
You can find that there is a merge request when you will try to rebase your pull request. You can rebase using the "rebase" button on the pull request page.
"Rebase" means to bring the other changes that has been merged into your version to ensure everything works fine together.
A merge conflict can be resolved on following the following steps:
- Update your local repository using "git fetch"
- Run a manual rebase using "git rebase"
- Resolve the conflicts using "git mergetool" or "git rebase --continue"
- Make code changes in the conflict part (list of files will be shown on command line)
- Commit the resolved code using "git commit --amend"
- Push a new patchset to Gerrit using "git push"
Commands:
Use the following command:
git fetch
This will bring in the latest code changes associated with a pull request in Gerrit.
Use the following command:
git rebase
This will attempt to rebase the pull request. This can be done using the Gerrit UI as well. In case of merge conflict, this step will fail and the only option is to resolve the merge conflict in console.
This command will list the files where there is a conflict. Make note the files as we need to open and resolve the conflicts one by one.
Fix the conflicts
Open the files one by one using vi and vim command and resolve the conflict. In each file, search for HEAD by press ESC key, then type "/" key and type HEAD followed by ENTER key.
This will should the first occurence of HEAD which is the starting of a conflict. Press N key to go to the next occurence.
A conflict looks like this:
<<<<<<< HEAD:file.txt
original code snippet
=======
conflicting code snippet
>>>>>>> commit-id:file.txt
Modify such parts as:
- Keep original code snippet, delete conflicting code snippet
- OR keep conflicting code snippet, delete original code snippet
- Keep a modified version of the code snippet
Once done, remove the lines that denote the conflicting lines like:
<<<<<<< HEAD:file.txt
=======
>>>>>>> commit-id:file.txt
Once fixing all merge conflicts in a file, get out of the file by pressing ESC key followed by :wq
Do this for all files with conflicting changes and add the changed files using git add command.
git add changed-file.txt
Once done, go on to the next step. Use the following command:
git rebase --continue
This will accept the new changes and continue the rebasing. If you have resolved the conflicts successfully, this step will pass. In case of error, fix the conficts as reported by this command and fix this step again.
Use the following command:
git commit --amend
This will commit the new changes without the merge conflict.
Use the following command:
git push
This will update your pull request with the rebased changes.
With this, you will be able to resolve merge conflicts which can happen if there are multiple developers working on the same project.
Sign up for FREE 3 months of Amazon Music. YOU MUST NOT MISS.