Posted on • Originally published athenriqueleite42.hashnode.dev
Git: How to fix PR conflicts
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 calledmaster
, and from this branch we created 2 new branches at the samepoint in time:feat/1
andfeat/2
.
Obs:point in time is not the same astime.Point in time means that no changes happened in the branch since the last time you checked. On the example above, themaster
branch keeps on the samepoint in time
until thePR #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 samepoint in time, git will not be able to know which one of the changes it must keep, the one ofPR #1
or the one ofPR #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 toconfigure 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
, pressEsc
,:wq
andEnter
- If you are using Ubuntu's default editor, press
Ctrl + O
,Enter
andCtrl + X
- If you are using
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! 😄
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse