2

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.
Sven
  • 69,403
  • 10
  • 107
  • 109
CreateSean
  • 1,286
  • 1
  • 21
  • 42
  • Can you describe the error that you are getting? – mockinterface Mar 22 '14 at 10:30
  • Will give exact error when i get home – CreateSean Mar 22 '14 at 11:04
  • in sourcetree I get this error: http://pastie.org/8958815 (too large for a comment) – CreateSean Mar 22 '14 at 12:21
  • That's why you should edit your original question, like I did for you right now. Comments are not really the way to add huge pieces of information, and external pastebins are no good way to add those as well. They might get lost. Putting everything you have and know into your question is the best way to make it useful for future reference. – Sven Mar 30 '14 at 15:32

1 Answers1

6

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.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250