Undoing a 'git push'
Solution 1:
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 last_known_good_commit:branch_name
or in your case
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
Solution 2:
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.
Solution 3:
git revert
is less dangerous than some of the approaches suggested here:
prompt> git revert 35f6af6f77f116ef922e3d75bc80a4a466f92650
[master 71738a9] Revert "Issue #482 - Fixed bug."
4 files changed, 30 insertions(+), 42 deletions(-)
prompt> git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#
nothing to commit (working directory clean)
prompt>
Replace 35f6af6f77f116ef922e3d75bc80a4a466f92650 with your own commit.
Solution 4:
A way to do it without losing the changes you wanted:
git reset cc4b63b
git stash
git push -f origin alpha-0.3.0
git stash pop
Then you can choose the files you meant to push