When working collaboratively in a Git repository, after committing some new changes and trying to push with:

git add [file]
git commit -m "[your message]"
git push

You might see the message:

failed to push some refs to [url].
Updates were rejected because the remote contains work that you do not have locally

Option 1: Resolve conflicts locally

Fetch the latest changes from the remote repository:

git fetch origin

Merge the remote version of the repository with the local version (assuming you are in the main branch):

git merge origin/main

You will likely see a message similar to:

CONFLICT (content): Merge conflict in your-file.md

Open the conflicting files and manually edit them. You will see annotations like this indicating where there are conflicts:

<<<<<<< origin/main
Change #1
===
Change #2
>>>>>>> main

Resolve the conflicts and remove the annotations:

Change #1
Change #2

Commit your changes and push the merged results back to the remote repository.

Option 2: Open a Pull Request

To resolve, create a new branch:

git checkout -b new-branch-name

Check that you are in the new branch:

git branch -v

Then push your new branch to the remote repository:

git push --set-upstream origin new-branch-name

Then create a Pull Request on Github (you will see a green button pop up “compare & pull request”)

Git will say “Can’t automatically merge”. That’s OK. Click “Create pull request”.

Git will “Check for ability to merge automatically” and will then say:

“This branch has conflicts that must be resolved”

Click “Resolve conflicts”

Git will show you text like this that shows the conflicting changes in each branch:

<<<<<<< new-branch-name
Yet another one
===
Another one
>>>>>>> main

Edit the text that you see to produce a corrected version of the file. You have to remove the text added by git.

Then, click “Mark as resolved” and “Commit merge”.

Then, “Merge pull request” and “Confirm merge”.

When you go back to the main page of your repository, you will see the new changes.

One more thing! On your computer, make sure to exit your branch and return to the main branch.

git checkout main

Then, pull your latest changes from the Github repository.

git pull

Congratulations, you have successfully resolved a merge conflict!