samwell
samwell

Reputation: 2797

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.

Upvotes: 109

Views: 113402

Answers (10)

10raw
10raw

Reputation: 574

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

Upvotes: 0

jglathe
jglathe

Reputation: 346

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.

Upvotes: 1

Danilo Souza Morães
Danilo Souza Morães

Reputation: 1593

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

git push origin master --delete
git push origin master

Upvotes: 3

Khaled AbuShqear
Khaled AbuShqear

Reputation: 1388

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**

Upvotes: 0

svick
svick

Reputation: 244948

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.

Upvotes: 166

filiph
filiph

Reputation: 3113

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.

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

Upvotes: 2

emery
emery

Reputation: 9703

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

Upvotes: 13

Sudheesh.M.S
Sudheesh.M.S

Reputation: 508

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

Upvotes: 0

richo
richo

Reputation: 8999

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.

Upvotes: 17

Chris Ledet
Chris Ledet

Reputation: 11628

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

git push origin master -f

Upvotes: 13

Related Questions