This is the issue I've had before on my own repository when I tried to push my local changes to my GitHub repository and I got this error as well
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes before pushing again. See the 'Note about fast-forwards'
section of git push --help for details.
In my view, it's okay to do within the first minute or so of a change you've pushed if you feel you've made a mistake.
If you've committed and immediately regret it, then do:
git reset HEAD~1
git push origin master -f
This is a "soft" reset, so it should basically turn your commit back into un-committed local changes to the files.
The reason you don't want to do this after that first minute or so is in case someone has merged it. It's not the end of the world--if people don't have any local changes it's trivial for them to get back on track (just reset --hard to the latest commit). And if someone is savvy with rebasing they'll be able to patch things up regardless.
Regardless of the couple minutes of leeway to fix a bad commit, if anyone has committed after you, then definitely do not force push.