Git: failed to push commits to the remote repository

Ronaldo B.August 1, 20221 min read

You may have already encountered this issue, when trying to execute the command git push origin master you get the following error: error: failed to push some refs to... and the link to the remote repository server.

This usually happens because you have newer changes on the remote that do not exist in the local repository, so you need to update the local and resolve conflicts if any exist before pushing to the remote.

To update the local run the command: git pull origin master

If the error still persists it means that the origins of both repositories are different and therefore you need to update the local with the flag --allow-unrelated-histories, meaning you allow git to fetch and merge the unrelated histories with the local repository.

So the complete command will be: git pull origin master --allow-unrelated-histories

After that you will be able to push or pull your commits normally.

NOTE: In this example we are assuming that your branch is master and the remote is named origin.

Keep learning with Hcode

Sign up for the course platform and follow the next blog guides.