When you’re doing a Git Merge, sometimes you have made changes to the same line of code that someone else also changed. When you try to merge your files you will get an error message that will look something like this:
When this happens, Git will create a special version of the files that had the conflicts that marks where the conflicts are. You have to go in and edit those files and resolve the conflicts, then commit your changes like any other change.
Here is a quick rundown of the process:
- Click on a file with merge conflicts (should be in separate Merge Conflicts area in Source Control)
- Edit the file to make it look like how it should be. Git will show the conflict by using the following dividers:
- <<<<<<<<< HEAD (this is what the original file has)
- ========= (this ends the original file and starts your changes)
- >>>>>>>>> name-of-branch (this ends your changes)
- Remember to remove the three dividers above. If you’re using GitLens you can click one of the three Accept links and it will automatically remove the dividers.
- Commit your changes
- Push your changes up to GitHub (you can do a git push or click the Sync Changes button in VS code).
And here it is in more detail with some images.
[1] Click on a file with merge conflicts (
They should be in the separate Merge Changes area in Source Control.
[2] Edit the file to make it look like how it should be.
Git will add three dividers to the file in each area where there are conflicts (there can be multiple of these per file depending on the number of conflicting lines in the file).
<<<<<<<<< HEAD (start original file code)
========= (end original and start your changes)
>>>>>>>>> name-of-branch (end your changes)
Here is an example:
You have two options to edit the file:
- If you have GitLens installed then you will be able to click one of thea Accept links:
- Accept Current Change: uses the existing code and removes yours.
- Accept Incoming Change: uses your code and removes existing code
- Accept Both Changes: keeps both sets of code
- Manually edit the file. You just go in and change the file to make it keep the lines of code you want.
[3] Remember to remove the three dividers.
This is if you did it manually. If you used the Accept links then it will happen automatically. Your file should look something like this with no dividers:
[4] Commit your changes
Do this like any other commit:
- Stage files (click the + by the files you changed)
- Write commit message
- Click to commit.
[5] Push your changes up to GitHub.
You can do a git push or click the Sync Changes button in VS code. It’s important to do this so your changes are saved there and if you’re in a group other people will be able to see your changes.