Skip to content

How to fix merge conflicts using git command line

Suppose you have a branch called feature/landing-page-business which was created from the develop branch.

After making and pushing some commits to feature/landing-page-business, you try to merge it with develop:

# Add the new changes and commit them git add . git commit -m "landing page completed" # Push the changes to the feature branch git push origin feature/landing-page-business # Switch to the develop branch and update if necessary git fetch && git checkout develop git pull # Merge the feature branch into develop git merge feature/landing-page-business
Code language: Bash (bash)

At this point you receive an error message indicating there are some merge conflicts.

What’s happened is that while you were working in your feature branch, some of the files that you changed had also changed in the develop branch.

You can find out about the files that need to be manually checked by using git status. This means going to each file and decided what parts of the code to keep or change.

When you’ve resolved the conflicts:

# Add the modified files git add . # Commit the merge conflict git commit -m "resolved merge conflict" # Switch the code to a temporary branch git branch conflict-fix-landing-page-business # Switch to the feature branch and merge the resolved conflict git checkout feature/landing-page-business git merge conflict-fix-landing-page-business
Code language: Bash (bash)

Finally you can merge the feature/landing-page-business branch into develop:

# Switch to develop and merge the feature branch into it git checkout develop git merge feature/landing-page-business
Code language: Bash (bash)
Published inGit