tags:

views:

3853

answers:

3
+12  Q: 

Undoing a git push

I just did a doodoo, and in need of some help :)

Here's what I did on my supposed-to-be-stable branch...

% git rebase master
First, rewinding head to replay your work on top of it...
Fast-forwarded alpha-0.3.0 to master.
% git status
# On branch alpha-0.3.0
# Your branch is ahead of 'origin/alpha-0.3.0' by 53 commits.
#
nothing to commit (working directory clean)
% git push
Fetching remote heads...
  refs/
  refs/heads/
  refs/tags/
  refs/remotes/
'refs/heads/master': up-to-date
updating 'refs/heads/alpha-0.3.0'
  from cc4b63bebb6e6dd04407f8788938244b78c50285
  to   83c9191dea88d146400853af5eb7555f252001b0
    done
'refs/heads/unstable': up-to-date
Updating remote server info

That was all a mistake as I later realized. I'd like to undo this entire process, and revert the alpha-0.3.0 branch back to what it was.

Could anyone point me in the right direction?


The following is the fix in action based on the accepted answer...

% git push -f origin cc4b63b:alpha-0.3.0
Fetching remote heads...
  refs/
  refs/heads/
  refs/tags/
  refs/remotes/
updating 'refs/heads/alpha-0.3.0' using 'cc4b63b'
  from 83c9191dea88d146400853af5eb7555f252001b0
  to   cc4b63bebb6e6dd04407f8788938244b78c50285
    done
Updating remote server info
%
A: 

this stackoverflow post perhaps?

Steen
It's not really the same situation, undoing a rebase is a local repository scenario, undoing a git push involves a remote repository and can be more tricky depending on the access you have.
Charles Bailey
Steen - you're right - I probably should have I suppose. I figured that the blessed repository that all pull from is more of an admin task and so belongs here, where general client-side git is a stackoverflow question.
Cyrus
+28  A: 

You need to make sure that no other users of this repository are fetching the incorrect changes or trying to build on top of the commits that you want removed because you are about to rewind history.

Then you need to 'force' push the old reference.

git push -f origin cc4b63bebb6:alpha-0.3.0

You may have receive.denyNonFastForwards set on the remote repository. If this is the case, then you will get an error which includes the phrase [remote rejected].

In this scenario, you will have to delete and recreate the branch.

git push origin :alpha-0.3.0
git push origin cc4b63bebb6:refs/heads/alpha-0.3.0

If this doesn't work - perhaps because you have receive.denyDeletes set, then you have to have direct access to the repository. In the remote repository, you then have to do something like the following plumbing command.

git update-ref refs/heads/alpha-0.3.0 cc4b63bebb6 83c9191dea8
Charles Bailey
A perfect and well explained response - thank you very much.For anyone else who stumbles accross this, for academic reasons I tried both of the first 2 approaches, and both worked - obviously if the first one works, it's the cleanest approach.If I chould UP you 10 times Charles, I would. :)
Cyrus
+1  A: 

I believe that you can also do this:

git checkout alpha-0.3.0
git reset --hard cc4b63bebb6
git push origin +alpha-0.3.0

This is very similar to the last method, except you don't have to muck around in the remote repo.

Benny Wong