How to Handle Merge Conflicts

Free Git Tutorial

Learn how to resolve merge conflicts in Git by viewing both sets of changes and deciding which to keep, with our step-by-step guide on handling them.

This exercise is excerpted from Noble Desktop’s Git & GitHub training materials and is compatible with updates through 2022. To continue learning web development with hands-on training, check out our coding bootcamps in NYC and live online.

Merging Changes

Sometimes you’ll get a conflict when pulling, reverting commits, merging branches, etc. For example, you and another developer unknowingly both work on the same part of a file. The other developer pushes their changes to the remote repo. When you then pull them to your local repo you’ll get a merge conflict. Luckily Git has a way to handle conflicts, so you can see both sets of changes and decide which you want to keep.

Full-Stack Web Development Certificate: Live & Hands-on, In NYC or Online, 0% Financing, 1-on-1 Mentoring, Free Retake, Job Prep. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.

What To Do With a Merge Conflict

  1. When you get a merge conflict, open a conflicted file. They are listed in the Source Control panel vscode soure control icon under Merge Changes.
  2. In the conflicted file, look for these conflict markers:

    <<<<<<< HEAD (Current Change)
    Marks the start of the changes.

    =======
    Divides your changes from the changes in the other branch.

    >>>>>>> branch-name (Incoming Change)
    Marks the end of the changes.

  3. Now you must decide how to resolve the conflict. Above the change you can choose various ways to handle it:

    • Accept Current Change
    • Accept Incoming Change
    • Accept Both Changes
    • Compare Changes

    Look at the code and choose whichever is appropriate for this case.
    After accepting the change, you can make further edits to the code as needed.

  4. Save the file.
  5. After you’ve handled all the conflicts, in the Source Control panel vscode soure control icon stage the changes by hovering over Merge Changes and clicking the plus (+) that appears to its right.
  6. At the top of the Source Control panel vscode soure control icon click the Commit button vscode commit icon.
  7. Push whenever you’re ready to upload those changes to GitHub so others can get them.

photo of Dan Rodney

Dan Rodney

Dan Rodney has been a designer and web developer for over 20 years. He creates coursework for Noble Desktop and teaches classes. In his spare time Dan also writes scripts for InDesign (Make Book JacketProper Fraction Pro, and more). Dan teaches just about anything web, video, or print related: HTML, CSS, JavaScript, Figma, Adobe XD, After Effects, Premiere Pro, Photoshop, Illustrator, InDesign, and more.

More articles by Dan Rodney

How to Learn Git

Master Git with hands-on training. Git is a free, open-source version control system that allows developers to track the changes they make to code.

Yelp Facebook LinkedIn YouTube Twitter Instagram