I had a repository that had some bad commits on it (D, E and F for this example).
A-B-C-D-E-F master and origin/master
I’ve modified the local repository specifically with a git reset --hard. I took a branch before the reset so now I have a repo that looks like:
A-B-C master
\ D-E-F old_master
A-B-C-D-E-F origin/master
Now I needed some parts of those bad commits so I cherry picked the bits I needed and made some new commits so now I have the following locally:
A-B-C-G-H master
\ D-E-F old_master
Now I want to push this state of affairs to the remote repo. However, when I try to do a git push Git politely gives me the brush off:
$ git push origin +master:master --force
Total 0 (delta 0), reused 0 (delta 0)
error: denying non-fast forward refs/heads/master (you should pull first)
To git@git.example.com:myrepo.git
! [remote rejected] master -> master (non-fast forward)
error: failed to push some refs to 'git@git.example.com:myrepo.git'
How do I get the remote repo to take the current state of the local repo?
If forcing a push doesn’t help (
git push --force originorgit push --force origin mastershould be enough), it might mean that the remote server is refusing non fast-forward pushes, via eitherreceive.denyNonFastForwardsconfig variable (see git config manpage for description), or via an update/pre-receive hook.With older Git you can work around that restriction by deleting
git push origin :master(note the:before branch name) and then re-creatinggit push origin mastergiven branch.If you can’t change this, then the only solution would be instead of rewriting history to create a commit reverting changes in D-E-F: