User:BhbeeX/Everybody struggles
MERGE CONFLICT: An open-source vocabulary term I didn't know before I applied to Outreachy
editAs someone new to pitching in on open-source projects, I didn't know anything about the term "Merge Conflict" and when I came across it during the contribution phase of the Outreachy application, it was one of my major challenges. After creating a branch and modifying some files under the project, I submitted a pull request (PR) only to be confronted with the notification; "This branch has conflicts that must be resolved."
After spending approximately 48 hours making this PR(not at a stretch 😅), it turns out my changes couldn't be merged because I couldn't figure out this issue. I remember giving up and shutting down my computer after trying and failing to sort it out multiple times. Frustrating stuff! 😭
We are encouraged by Outreachy to reach out to our mentor(s) when encountering blockers after doing our research for a solution without success. So I surfed the net to understand what this term meant and the solution I needed!
What is a Merge Conflict?
editIn version control systems like Git, a merge conflict arises when two branches have diverged, and changes made in one branch conflict with changes made in another branch. This commonly occurs when distinct developers independently modify the same section of a file. During the merging process, the version control system faces difficulty automatically deciding which changes to integrate, resulting in a conflict that needs manual resolution.
According to GitHub's documentation, Git can often resolve differences between branches and merge them automatically. Usually, the changes are on different lines, or even in different files, which makes the merge simple for computers to understand. However, sometimes there are competing changes that Git can't resolve without your help. Often, merge conflicts happen when people make different changes to the same line of the same file, or when one person edits a file and another person deletes the same file. You can read more here: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/addressing-merge-conflicts/about-merge-conflicts
Resolving merge conflicts
editYou must resolve all merge conflicts before you can merge a pull request on GitHub. As a developer, you need to manually resolve the conflicts by choosing which changes to keep. You can view a list of the files with conflicting changes above the Merge pull request button. In Git, when a conflict occurs, you might see conflict markers in the affected files. For example:
<<<<<<< HEAD
// Changes made in the current branch
=======
// Changes made in the incoming branch
>>>>>>> incoming_branch
You need to edit the file to remove the conflict markers and choose the final version of the code.
To avoid or minimize conflicts, it's a good practice to regularly pull changes from the main branch into your working branch to keep it up-to-date. To do this, follow the steps below:
1. If you haven't, add the upstream repository as a remote in your local Git repository. This remote points to the original repository you forked from GitHub
git remote add upstream <upstream_repository_url>
2. Regularly fetch and merge: To update your branch with the latest update from the target branch, fetch the upstream changes and merge them into your local branch.
# fetch the latest change from the upstream repository.
git fetch upstream
# switch to your branch
git checkout <your_branch_name>
# merge your changes with the target branch(e.g main)
git merge upstream/main
3. Finally, commit the changes to complete the merge process and push your changes
git commit
git push
By following these steps, you should be able to successfully resolve merge conflicts in Git.
And that was how I was able to figure it out and show kindness by helping my fellow contributors who were facing the same challenge of a "merge conflict".
It is essential to communicate during the contribution phase about what task you're working on to ensure that someone else isn't working on the same file(especially the same lines of that file). This is mostly the reason for a merge conflict. Once one of the contributor's PR has been merged before yours and you also made changes to that same file or set of files, you are most likely to encounter a merge conflict and vice versa.
If you peradventure still do not know how to fix your blockers after doing your research, know that everyone also faces one or more challenges and it is very much welcome to reach out to your mentors and fellow contributors for help.