Undoing a 'git push'

GitGit Push

Git Problem Overview


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.

What should I do?

Git Solutions


Solution 1 - Git

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 - Git

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

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 - Git

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

Solution 5 - Git

The accepted solution (from @charles bailey) is highly dangerous if you are working in a shared repo.

As a best practice, all commits pushed to a remote repo that is shared should be considered 'immutable'. Use 'git revert' instead: http://www.kernel.org/pub/software/scm/git/docs/user-manual.html#fixing-mistakes

https://git-scm.com/book/be/v2/Git-Basics-Undoing-Things

Solution 6 - Git

git push origin +7f6d03:master

This will revert your repo to mentioned commit number

Solution 7 - Git

Another way to do this:

  1. create another branch
  2. checkout the previous commit on that branch using "git checkout"
  3. push the new branch.
  4. delete the old branch & push the delete (use git push origin --delete <branch_name>)
  5. rename the new branch into the old branch
  6. push again.

Solution 8 - Git

Undo multiple commits git reset --hard 0ad5a7a6 (Just provide commit SHA1 hash)

Undo last commit

git reset --hard HEAD~1 (changes to last commit will be removed ) git reset --soft HEAD~1 (changes to last commit will be available as uncommited local modifications)

Solution 9 - Git

Scenario 1: If you want to undo the last commit say 8123b7e04b3, below is the command(this worked for me):

git push origin +8123b7e04b3^:<branch_name>

Output looks like below:

Total 0 (delta 0), reused 0 (delta 0)
To https://testlocation/code.git
 + 8123b7e...92bc500 8123b7e04b3^ -> master (forced update)

Note: To update the change to your local code (to remove the commit locally as well) :

$ git reset --hard origin/<branchName>
Message displayed is :    HEAD is now at 8a3902a comments_entered_for_commit

Additional info: Scenario 2: In some situation, you may want to revert back what you just undo'ed (basically undo the undo) through the previous command, then use the below command:

git reset --hard 8123b7e04b3
git push

Output:

HEAD is now at cc6206c Comment_that_was_entered_for_commit

More info here: https://github.com/blog/2019-how-to-undo-almost-anything-with-git

Solution 10 - Git

git reset --hard HEAD^
git push origin -f

This will remove the last commit from your local device as well as Github

Solution 11 - Git

you can use the command reset

git reset --soft HEAD^1

then:

git reset <files>
git commit --amend

and

git push -f

Solution 12 - Git

The existing answers are good and correct, however what if you need to undo the push but:

  1. You want to keep the commits locally or you want to keep uncommitted changes
  2. You don't know how many commits you just pushed

Use this command to revert the change to the ref:

git push -f origin refs/remotes/origin/<branch>@{1}:<branch>

Solution 13 - Git

To revert the push

git reset --hard HEAD@{1}
git push -f
git reset --hard HEAD@{1}

now your local will be ahead to remote

git reset --hard origin/master

or alternative way

  1. To reset the push: git reset --soft HEAD^1

  2. Will appear modify file so reset them: git reset <files>

  3. git commit --amend

  4. git push -f

Solution 14 - Git

I had the same issue. I just copy the last commit id(af12de...) which I wanted to revert. Then executes this command git revert af12de.... Then push my changes to the master. This worked for me

Solution 15 - Git

Folks using gitlab have a button called "options" which contains "revert".

Solution 16 - Git

If you want to ignore the last commit that you have just pushed in the remote branch: this will not remove the commit but just ignoring it by moving the git pointer to the commit one earlier, refered by HEAD^ or HEAD^1

git push origin +HEAD^:branch

But if you have already pushed this commit, and others have pulled the branch. In this case, rewriting your branch's history is undesirable and you should instead revert this commit:

git revert <SHA-1>
git push origin branch

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCyrusView Question on Stackoverflow
Solution 1 - GitCB BaileyView Answer on Stackoverflow
Solution 2 - GitBenny WongView Answer on Stackoverflow
Solution 3 - GitneoneyeView Answer on Stackoverflow
Solution 4 - GitcurmilView Answer on Stackoverflow
Solution 5 - GitSabooshView Answer on Stackoverflow
Solution 6 - Gitireshika piyumalieView Answer on Stackoverflow
Solution 7 - GitRushabh MehtaView Answer on Stackoverflow
Solution 8 - GitJayminLimbachiyaView Answer on Stackoverflow
Solution 9 - GitBarani rView Answer on Stackoverflow
Solution 10 - GitRakib NoushadView Answer on Stackoverflow
Solution 11 - Gituser7396942View Answer on Stackoverflow
Solution 12 - GitVlad274View Answer on Stackoverflow
Solution 13 - GittitleLoginView Answer on Stackoverflow
Solution 14 - GitchetyView Answer on Stackoverflow
Solution 15 - GitJavaQuestView Answer on Stackoverflow
Solution 16 - GitmkebriView Answer on Stackoverflow