DEV Community

loading...
Cover image for How to resolve merge conflicts with git

How to resolve merge conflicts with git

Stacie Taylor
・2 min read

Dealing with messy merge conflicts can be a bit like navigating heavy traffic. With a bit of patience, forward-thinking, and sometimes a pal, you can resolve any conflict you're faced with. Take a deep breath and follow the steps below to get those resolutions in place!

Step 1

Pull down all the latest code from the main branch of your remote repo.

git fetch

Step 2

Merge all that fresh new code into your feature branch locally.

git rebase origin/main

Step 3

[Optional] Run this to get a high-level look at which files have conflicts.

git status

Step 4

Fix all the merge conflicts in your project.

  • Go to those files in your editor. You will see that it shows you the code that lives on the HEAD and your branch's code.
  • Keep the good code that you need when it asks you to choose and delete the code you don't want. This can be the most challenging part because you might be seeing code from your team that you don't recognize -- pair with your pals to decide what code goes and what code stays if necessary.
  • When you see that the files that previously had conflicts are squared away, save them!

Step 5

Stage all the files that you fixed.

git add .

Step 6

Continue the rebase to see what's next.

git rebase --continue

Step 7

You may still have more conflicts to work through because the rebase is going to compare every commit that came down from the main branch to your feature branch. Keep the following steps 3-6 until you're taken back to your feature branch.

Step 8

Push your updated code up to your remote repository.

git push --force-with-lease

Note: --force-with-lease is a safer option than force because it will not overwrite any work on the remote branch if more commits were added to the remote branch. Read more here.

Discussion (2)

Collapse
darkwiiplayer profile image
DarkWiiPlayer

An easier way of doing it:

git pull
git mergetool

😜

Collapse
piotrs profile image
Piotr S

Thanks!

Forem Open with the Forem app