Topic 4

Conflict

A conflict is not a disaster. It simply means Git needs help choosing between overlapping edits.

What a conflict means

A conflict appears when two sets of changes touch the same lines or the same small region of a file. For a push or pull to succeed cleanly, the file on GitHub and the local file you started editing from should match.

If they are different, pushing or pulling will result in a conflict (refer to the picture below). Git stops and asks you to decide what the final text should be. Let’s learn how to resolve this issue.

VS Code warning that a push was rejected and a pull is required

After that warning, click ‘Cancel’. Execute the command “pull”.

Then you will be able to see the following screen where the staging will be canceled. The green box displays the modifications made locally, while the blue box displays the content from GitHub.

VS Code source control panel after staging is canceled during a merge conflict

Three options

There are three options, and you can click the one you prefer:

Option What happens
Accept Current Change Only the modifications made locally will be accepted, and the content on GitHub will be discarded.
Accept Incoming Change The modifications made locally will be discarded, and only the content on GitHub will be accepted.
Accept Both Changes Both the local and GitHub contents will be accepted. This is the safest option.
VS Code conflict options for accepting current, incoming, or both changes

For example, if you click on ‘Accept Both Changes’, you will be able to see a screen similar to the one on the right. Now, execute the compilation (or saving).

VS Code conflict editor with Accept Both Changes highlighted
VS Code editor after both conflict changes are accepted

Finish the merge

In the source control, click on the ‘+’ button in ‘Merge Changes’, and also click on the ‘+’ button in ‘Changes’. Then you will be able to see that all the changes have been staged, just like the screen on the right.

VS Code Source Control with Merge Changes ready to stage
VS Code Source Control with Changes ready to stage
VS Code Source Control after all conflict changes are staged

Proceed with the command “Commit & Push”. Thus, the conflict has been resolved.

VS Code Source Control menu with Commit & Push highlighted

If you can explain what the final file should say and the repository returns to a clean state afterward, the conflict has been handled correctly.