Understanding how to resolve merge conflicts in Git is an essential skill for developers. Merging changes, addressing conflicts, and committing these changes are crucial aspects of collaboration and version control in software development.
Key Insights
- Merge conflicts in Git often occur when two developers unknowingly work on the same part of a file and attempt to push their changes to the remote repository.
- Upon encountering a merge conflict, developers should open the conflicted file, which would be listed under 'Merge Changes' in the Source Control panel.
- Within the conflicted file, developers will find conflict markers signifying the start of changes (<<< HEAD), a division between changes from different branches (=======), and the end of changes (>>> branch-name).
- To resolve the conflict, developers must decide between accepting the current change, the incoming change, accepting both changes, or comparing changes. Further edits can be made to the code as needed after accepting the change.
- Once all conflicts are resolved, the changes must be staged in the Source Control panel by hovering over 'Merge Changes' and clicking the plus (+) that appears to its right.
- After staging, developers must commit the changes using the 'Commit' button at the top of the Source Control panel. Push the changes to GitHub when ready to upload.
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.
What To Do With a Merge Conflict
- When you get a merge conflict, open a conflicted file. They are listed in the Source Control panel
under Merge Changes.
-
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. -
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. - Save the file.
- After you’ve handled all the conflicts, in the Source Control panel
stage the changes by hovering over Merge Changes and clicking the plus (+) that appears to its right.
- At the top of the Source Control panel
click the Commit button
.
Push whenever you’re ready to upload those changes to GitHub so others can get them.