Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Henrique Leite
Henrique Leite

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

git conflict

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 themaster 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 usingvim, pressEsc,:wq andEnter
    • If you are using Ubuntu's default editor, pressCtrl + 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! 😄

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Location
    Brazil
  • Joined

More fromHenrique Leite

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp