-
Notifications
You must be signed in to change notification settings - Fork 0
Merge & Conflict resolution
Handling merges and resolving conflicts effectively is key to maintaining a clean and stable codebase. This page breaks down the process for merging branches and resolving conflicts when they arise, ensuring smooth collaboration among team members.
- Keep branches up-to-date: Before merging a feature branch into main or develop, make sure your branch is up-to-date with the target branch by regularly merging or rebasing from it.
- Small, frequent merges: Avoid large or long-lived branches by merging small, completed features frequently. This reduces the likelihood of conflicts and simplifies the merge process.
- Test before merging: Ensure that all tests pass and no breaking changes are introduced before merging. Use continuous integration (CI) to automate these checks.
Follow these steps to merge a branch :
-
Update your feature branch :
-
Before merging your branch into the target branch (
mainordevelop), first update your branch by running :git fetch git pull origin <target-branch>
-
-
Resolve any conflicts (if they arise) during the update (see the next section for conflict resolution).
-
Run tests :
- Ensure that the application builds successfully and that all tests pass.
./gradlew check connectedCheck -
Merge the Branch :
-
If there are no conflicts, and all tests pass, proceed with the merge:
git checkout <target-branch> git merge <your-branch>
-
-
Push the Changes :
-
Push the changes to the remote repository:
git push origin <your-branch>
-
-
Delete the Feature Branch (Optional) :
-
Once the branch is merged, you can delete the feature branch locally and remotely:
git branch -d <your-branch> git push origin --delete <your-branch>
-
Conflicts occur when multiple branches modify the same lines of code or files. Here's how to handle conflicts when they arise:
-
Identify the Conflict :
- Git will notify you about the files with conflicts during the merge or rebase process. The conflict markers look like this:
<<<<<<< HEAD Your changes ======= Changes from the other branch >>>>>>> branch-name
- Git will notify you about the files with conflicts during the merge or rebase process. The conflict markers look like this:
-
Manually Resolve the Conflict :
- Open the conflicting file(s) and decide which changes to keep. You can:
- Keep your changes by removing the other branch's lines.
- Keep the other branch's changes by removing your lines.
- Combine both changes by manually editing the file.
- Remove the conflict markers <<<<<<<, =======, and >>>>>>> after you make your choice.
- Open the conflicting file(s) and decide which changes to keep. You can:
-
Stage the Resolved Files :
- After resolving conflicts, stage the changes using:
git add <file-name>
- After resolving conflicts, stage the changes using:
-
Continue the Merge or Rebase :
- After staging the resolved files, continue the process:
-
For a merge:
git commit
-
For a rebase:
git rebase --continue
-
- After staging the resolved files, continue the process:
-
Run Tests :
- Ensure everything is working correctly by running tests again before completing the merge:
./gradlew check connectedCheck
- Ensure everything is working correctly by running tests again before completing the merge:
-
Push Changes :
- Once conflicts are resolved and all tests pass, push the changes:
git push origin <target-branch>
- Once conflicts are resolved and all tests pass, push the changes:
By following these steps, you can handle merges and resolve conflicts efficiently, ensuring minimal disruption to the project. Good communication and regularly updating your branch with the target branch will prevent conflicts from becoming overwhelming.