Question

I'm just starting with Git and I've got something that I need to reverse to a previous commit. I can do this locally no problem.

git reset --hard hashcode (or using sourcetree)

However when I try to push to bitbucket, souretree is gives me an error until I pull the previous commits down and I'm back where I started unable to revert to hte previous commit.l

How do I revert to a previous commit locally and then push to bitbucket overriding and removing all the commits that came after the one I'm reverting to?

The error message is:

git -c diff.mnemonicprefix=false -c core.quotepath=false push -v --tags origin Development:Development
Pushing to git@bitbucket.org:Username/project.git

To git@bitbucket.org:Username/project.git
 ! [rejected]        Development -> Development (non-fast-forward)
error: failed to push some refs to 'git@bitbucket.org:Username/project.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.
Was it helpful?

Solution

You simply can force the push:

git push -f bitbucket

But that assumes that nobody has alredy pulled from your BitBucket repo (or they will have to reset their local branch to the new remote history).
Plus, once a push is forced, there is no way to know who changed that history on the remote side.

This is consistent with your error message:

To git@bitbucket.org:Username/project.git
 ! [rejected]        Development -> Development (non-fast-forward)
error: failed to push some refs to 'git@bitbucket.org:Username/project.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.

Here is an extract of "git push Note about fast-forwards":

In such a case, and only if you are certain that nobody in the meantime fetched your earlier commit A (and started building on top of it), you can run "git push --force" to overwrite it.
In other words, "git push --force" is a method reserved for a case where you do mean to lose history.


The other (safest) option is to use git revert (even for a range of commits), to create a new commit (cancelling an old one), that you can push.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top