Join my free Bash scripting course on Udemy 👀

Fix failed to push some refs error when pushing to git remote

April 17, 2022  ‐ 2 min read

Pushing to a git remote may show you a frightening message sometimes, saying that your push is rejected by the remote.

$ git push
To github.com:koenwoortman/some-repo.git
 ! [rejected]        main -> main (non-fast-forward)
error: failed to push some refs to 'git@github.com:koenwoortman/some-repo.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

All this means is that your local branch is one or multiple commits behind compared to the branch you are pushing to. Maybe another developer pushed his or her commits? Or was it you from another machine? The GitHub dependabot might have been the cause as well.

Anyway, before you can push directly you need to get those commits you are lacking. I personally prefer to rebase (depending on the amount conflicts I am expecting):

$ git fetch && git rebase origin/{the branch you need}
First, rewinding head to replay your work on top of it...
Applying: Setup unit tests

After the rebase you should be good to go when pushing again.

Running a git pull should work as well, but I don't like the additional merge commits that are included in that approach.

You could take another route and not directly push, but create a new branch and make a pull request. This requires you to push your changes to a new remote branch first and then create the pull request.