1

I accidentally pushed a project to Git using git push when I intended to simply have it on my local repository (should've stopped after git commit).

I want to undo this git push such that the server repo goes back to a specific commit. I have looked at other questions such as this one, but the git push -f doesn't work for me and pulls up the error: To prevent you from losing history, non-fast-forward updates were rejected. I even tried

git push -f origin CommitSHA:mainline

I thought the -f tag would force the push regardless of whether I was pushing a previous commit or not. How should I fix this error so that I can undo the git push?

1 Answers1

0

Isn't your message missing something? I believe it should say something like

Merge the remote changes (e.g. 'git pull') before pushing again

after the part about rejecting your push. So, that would mean the remote branch may have more recent changes than your local one, e.g. someone else pushed changes. In that case, I would advice not to force push since you may throw away commits of other people.

You could either:

  • Do it anyway (git pull first should work)
  • Use git revert instead to undo your commit. This will ultimately introduce a second commit reverting your first one, hence both of them will be in the history (https://git-scm.com/docs/git-revert). That's not ideal, but rewriting history on remote branches never is.
SVSchmidt
  • 6,269
  • 2
  • 26
  • 37