How to force push a reset to remote repository?

Git

Git Problem Overview


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.

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

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.

Solution 3 - Git

Try using the -f flag and putting it after the remote branch name.

git push origin master -f

Solution 4 - Git

Steps to permanently enable force push in the following style

git push -f myrepo my-branch

Edit the file named "config" in the folder ending in ".git" on your remote repository

In git's command-line output from the failed push, look for the line that says something like:

error: failed to push some refs to 'ssh://[email protected]/srv/git/myrepo.git

then

ssh [email protected]
cd /srv/git/myrepo.git
vi config

Set "denyNonFastforwards" to false

In "config", set

[receive]
        denyNonFastforwards = false

Now you can push from your local machine with -f

git push -f myrepo my-branch

Solution 5 - Git

The best way around this is to delete the remote branch and re-send it:

git push origin master --delete
git push origin master

Solution 6 - Git

You're not allowed to do git push that is not fast-forward.

  1. If the remote is GitHub, go to https://github.com/$USER/$REPO/settings/branches and un-protect the branch in question.

enter image description here

You have to be admin of the repo to do that.

  1. If the remote is your own git server, run git config receive.denynonfastforwards false there.

Solution 7 - Git

The problem occurs as the current branch isn't configured properly for the PULL. First check whether the upstream branch is properly configured for the pull using - git remote show origin. You can find it under the section - Local branches configured for 'git pull':. If not, configure it using:

git config branch.MYBRANCH.merge refs/heads/MYBRANCH

Give appropriate branch name for the place holder - MYBRANCH

Solution 8 - Git

I'm using this group of commands to reset my remote repo, this will re-initialize your local repo and relink with your remote repo then force push the updates.

I think this way won't work in your case, but may be useful for someone else

go to the source folder then run the commands : note that https://github.com/*.git is your remote repo link

git init
git remote add origin https://github.com/*.git
git add .
git commit -m "initial commit"
git push origin master -f
git push --set-upstream origin master

**Note: this will clear all your git history on your master branch**

Solution 9 - Git

For me, @svick 's hint pointed in the right direction. Since the git server I wanted to modify is actually my box, I logged into it and did a git config --global receive.denynonfastforwards false to change all repos to accept a forced non-ff push. Didn't work out of the box. What I found was that in the config there already was receive.denynonfastforwards=true set, and it couldn't be erased with git config --global --unset receive.denynonfastforwards. Doing the edit in the repo manually (vi config) worked, though.

Solution 10 - Git

I solved by removing the master branch from protected and also default which is just over proted branch rules in setting of a repository.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsamwellView Question on Stackoverflow
Solution 1 - GitsvickView Answer on Stackoverflow
Solution 2 - GitrichoView Answer on Stackoverflow
Solution 3 - GitChris LedetView Answer on Stackoverflow
Solution 4 - GitemeryView Answer on Stackoverflow
Solution 5 - GitDanilo Souza MorãesView Answer on Stackoverflow
Solution 6 - GitfiliphView Answer on Stackoverflow
Solution 7 - GitSudheesh.M.SView Answer on Stackoverflow
Solution 8 - GitKhaled AbuShqearView Answer on Stackoverflow
Solution 9 - GitjglatheView Answer on Stackoverflow
Solution 10 - Git10rawView Answer on Stackoverflow