Step-by-Step guide to Resolving Merge Conflicts in GIT

ferozekhanferozekhan
3 min read

When working on a coding project, there might be times where team members, end up making changes to the same file with different contents. This may result in creating conflicting changes on the two feature branches, and then attempting a merge, will result in a merge conflict that needs to resolved manually before committing the code changes to the current branch. Here is a step-by-step guide to resolving a merge conflict in Git.

Step 1: Set Up Your Repository

  1. Initialize a Git Repository

     mkdir merge-conflict-demo
     cd merge-conflict-demo
     git init
    

  2. Create a File and Make an Initial Commit

     echo "Initial content" > file.txt
     git add .
     git commit -m "Initial commit"
    

Step 2: Create a Branch and Make Changes

  1. Create a New Branch

     git checkout -b feature1
    
  2. Modify the File and Commit on Feature Branch

     echo "Feature branch content" >> file.txt
     git add .
     git commit -m "Add feature1 changes"
    

Step 3: Make Conflicting Changes on Main Branch

  1. Switch Back to the Main Branch

     git checkout main
    
  2. Modify the File in a Conflicting Way and Commit

     echo "Main branch conflicting content" >> file.txt
     git add .
     git commit -m "Add conflicting changes on main"
    

Step 4: Merge Feature Branch into Main Branch

  1. Attempt to Merge the Feature Branch

     git merge feature1
    

Step 5: Resolve the Merge Conflict

  1. Identify the Conflict

    • Git will output something like:

  1. Open the Conflicted File

    • The conflicted file (file.txt) will have conflict markers:

Everything between the lesser than (<<<<<<<) and equals signs (=======) is the change in the current branch (the branch you're merging into). Everything between the equals (=======) and greater than () signs is the incoming change from the feature branch you want to merge to the current branch.

You may now remove those unwanted annotations and make changes to the file contents as desired.

  1. Manually Resolve the Conflict

    • Edit the file to resolve the conflict. For example, you might change it to:

  1. Add the Resolved File

    git add .
    
  2. Commit the Merge

    git commit -m "Resolve merge conflict between main and feature-branch"
    

Step 6: Verify the Merge

  1. Check the Commit History

    git log --oneline
    git log --oneline --graph
    
    • You should see the merge commit with both branches converging.

Additional Tips on Avoiding Merge Conflicts

Merge conflicts aren’t exactly fun, so here are some tips to avoid them:

  1. Communicate with your team members and collaborators often to avoid working on the same files at the same time! Let each other know who is working on what.

  2. Commit often with small changes.

  3. Try pulling before committing.

Summary

You've successfully resolved a merge conflict. This process ensures that changes from both branches are correctly integrated into the project.

That's all for today. Kindly share with the community. Until I see you next time. Cheers !

0
Subscribe to my newsletter

Read articles from ferozekhan directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

ferozekhan
ferozekhan