How Do I Resolve Merge Conflicts in a Git Repository?

Better Stack Team
Updated on June 24, 2024

Resolving merge conflicts in a Git repository involves manually resolving conflicting changes between branches. Here's a general overview of the process:

Step 1: Identify Merge Conflict

When you attempt to merge branches and Git detects conflicting changes, it will notify you about the conflicts. You'll see messages like:

 
Auto-merging <file>
CONFLICT (content): Merge conflict in <file>

Step 2: View Conflicts

Open the conflicting file(s) in your text editor. Inside, you'll see markers indicating the conflicting changes, like:

 
<<<<<<< HEAD
// Changes from your current branch
=======
// Changes from the branch being merged
>>>>>>> branch-name

Step 3: Resolve Conflicts

Manually edit the conflicting sections to resolve the conflicts according to your needs. You may choose to keep one set of changes, combine them, or make entirely new changes. Remove the conflict markers (<<<<<<<, =======, >>>>>>>) once you're done.

Step 4: Add Resolved Files

After resolving conflicts, add the modified files to the staging area using:

 
git add <resolved-file>

Step 5: Complete the Merge

Once all conflicts are resolved and staged, complete the merge by committing the changes:

 
git commit

Step 6: Verify and Push

After committing, verify that the merge was successful and push your changes if necessary:

 
git status
git push

Note:

  • During conflict resolution, you can use git status to check which files are in conflict and which have been resolved.
  • In case you want to abort the merge and start over, you can use git merge --abort.
Got an article suggestion? Let us know
Explore more
Git
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github