Table of contents
When working with PRs, we may encounter conflicts trying to merge them. In this article, we will learn what conflicts are and how to fix them.
What causes conflicts
In the image above, you can see that we have a main branch called master
, and from this branch we created 2 new branches at the same point in time: feat/1
and feat/2
.
Obs: point in time is not the same as time. Point in time means that no changes happened in the branch since the last time you checked. On the example above, the master
branch keeps on the same point in time
until the PR #1
is merged and the state of the branch changes.
After we created both branches (one can be created by you, and another one by your coworker), we start to make changes on them to develop a new feature, fix a bug, or improve the documentation, anything that we want to change.
As things on each branch are different, one of them will probably be faster to develop and will have the PR merged first, and once in a while may happen that 2 branches must change the same file.
As both branches were created at the same point in time, git will not be able to know which one of the changes it must keep, the one of PR #1
or the one of PR #2
(your PR), and it will cause the conflict.
How to fix the conflict
If the git commands below don't work, it may be because you need to configure your git before executing them.
Go back to the
master
branch (or the source branch for your current branch)git checkout master
Get the updated state
git pull
Go back to your branch
git checkout feat/2
Rebase with master
git rebase master
See the files that are causing conflicts
git status
Go to the files and make the necessary change to the file to include both your changes and the previous changes.
Add the files to be tracked by git
git add .
Continue the rebase
git rebase --continue
[May not occur] If a strange text appears, run the commands:
If you are using
vim
, pressEnter
,:wq
andEnter
If you are using Ubuntu's default editor, press
Ctrl + O
,Enter
andCtrl + X
Push the changes
git push
Conclusion
And that's it! Solving conflicts is not the end of the world, we can fix them very quickly. Hope that this article helped you in some way, and if you have anything to say or an experience to share, please say it in the comments! ๐