Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can not push changes after using git reset --hard

Tags:

git

git-revert

I had a mistake and commit some changes to git which I should not have committed. After I made the commit, I pushed my changes. I then used the following commands to try and reset my changes.

 git reset --hard head 

Now I want to push this 'reset' to the remote repository with this command:

git push MyBranch 

But I am getting this error:

remote: error: denying non-fast-forward refs/heads/branch (you should pull first) 

I tried to use this command without any success:

git push -f "origin"  

Any idea what I can do?

like image 988
mans Avatar asked Mar 21 '12 11:03

mans


People also ask

How do I push changes after resetting git hard?

You then can push that new commit, as a fast-forward change. @user654019: by creating a new commit composed of changes cancelling your previous commit: use git revert: you will then be able to push that new commit as a fast-forward push.

Can you undo a hard git reset?

In most cases, yes. Depending on the state your repository was in when you ran the command, the effects of git reset --hard can range from trivial to undo, to basically impossible.

Why git push is not working?

If git push origin master not working , all you need to do is edit that file with your favourite editor and change the URL = setting to your new location. Assuming the new repository is correctly set up and you have your URL right, you'll easily be able to push and pull to and from your new remote location.

How do I commit after hard reset?

In order to hard reset to the commit right before HEAD, use “git reset” with the “–hard” option and specify HEAD^. As you can see, the HEAD of the release branch is now pointing to the second commit : we essentially have reset to the commit before HEAD.


1 Answers

git push -f origin myBranch  

should work (provided you are aware this can be dangerous if MyBranch was already fetched by others in their own repo)

Note: if your remote repo ('origin') has its config set with

receive.denyNonFastForwards true 

it will deny any non fast-forward push (even when forced).
See "Is there a way to configure git repository to reject 'git push --force'?".


The OP user654019 reports

I managed to solve the problem this time by setting denyNonFastForwards to false and using -f to force the push

If the OP didn't have access to the repo, he/she would have to:

  • reset the local HEAD to its original position (see "Recover from git reset --hard?"):
    git reset HEAD@{1}
  • make a new commit which cancel your merge, as described in the ProGit book, with git revert:
    git revert -m 1 HEAD~ (in your case)

By example:

$ git revert -m 1 [sha_of_C8] Finished one revert. [master 88edd6d] Revert "Merge branch 'jk/post-checkout'"  1 files changed, 0 insertions(+), 2 deletions(-) 

revert a merge

A complete discussion on how to revert a merge can be found here.

The idea remains to generate only new commits, including one reverting the changes introduced by the merge commit.
You then can push that new commit, as a fast-forward change.

like image 157
VonC Avatar answered Sep 23 '22 08:09

VonC