How to force push a reset to remote repository?
Our remote master branch somehow got messed up. Current development code is on the master branch along with the latest commits. Obviously, the development code is not ready for the master branch.
So on my local repository, I did a reset to the latest tag, git reset --hard (Tag)
. The master branch is now correct on my local repository. Now when I try to push the changes on to the remote repository, git push origin master
, I get an error:
To (REMOTE GIT REPOSITORY LOCATION)
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to '(REMOTE GIT REPOSITORY LOCATION)'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again. See the
'Note about fast-forwards' section of 'git push --help' for details.
So after looking around I found out the --force
option. So I did a force push on to the remote repository, git push --force origin master
, and I still got an error:
Total 0 (delta 0), reused 0 (delta 0)
remote: error: denying non-fast-forward refs/heads/master (you should pull first)
To (REMOTE GIT REPOSITORY LOCATION)
! [remote rejected] master -> master (non-fast-forward)
error: failed to push some refs to '(REMOTE GIT REPOSITORY LOCATION)'
I can't do a pull on master, because it contains development code which can't be on master.
The message means that you're not allowed to do non-fast-forward push.
Your remote repository has most likely denyNonFastforwards = true
in its config. If you change that, git push --force
should work.
To change the setting, you need access to the machine with the remote repository. From there, do git config receive.denynonfastforwards false
.
The remote doesn't allow non-fast-forwards.
Your best option is to git revert
all of the commits that shouldn't be there and be more careful in future.
git revert [commit]
will create a new commit that undoes whatever [commit]
did.