Git push origin master after performing hard reset

1k Views Asked by At

I recently performed a hard reset to another branch, as I was unable to get my master branch back to a working state. I did this using the command:

git reset --hard masterBackupBeforeMerginDateDepositReceived04-01-2017

(i.e. to the branch where the particular feature I had broken was still working)

I then ran:

git commit -m 'commit after getting adds/omits working again'

having checked that the broken feature was working again on my local master branch- it was, so I wanted to commit this again to be sure I had that working state saved.

I then ran

git push origin master

to push the master branch to the server again, so that the now fixed feature should start working again on the live version. However, after running this push, Git displayed a message I've not seen before:

To [email protected]:abc/xyz.git ! [rejected] master -> master (non-fast-forward) error: failed to push some refs to '[email protected]:abc/xyz.git' hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Integrate the remote changes (e.g. hint: 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.

It seems that Git is not happy with me pushing an 'old' version to the server, when there is a 'newer' version currently on the server... How can I force the server to accept my push of the 'old' version (which is currently the master branch on my local machine?

3

There are 3 best solutions below

3
On

simply add the force option -f

git push -f origin master

Git Push Documentation

8
On

If you use reset and then force the push it can mess up other people who have pulled the version you don't like.

After the broken version has been pushed to the remote repo, it is best to recover using revert instead of reset.

Revert will add changes on top of the bad commit, such that the new version looks the way it did earlier. Then when you push you are just pushing new changes which fix the mistakes that were pushed earlier.

For example

git revert HEAD

will add new changes which undo the effect of the most recent commit. See the revert documentation for more details on undoing multiple commits.

0
On

If you want to push a local branchx to remote master branch, you can try git push -f origin branchx:master.

If you have already rename the local branchx to master, you can use git push -f origin master.