329

I have performed git commit followed by a git push. How can I revert that change on both local and remote repositories?

$ git log
commit 364705c23011b0fc6a7ca2d80c86cef4a7c4db7ac8
Author: Michael Silver <Michael [email protected]>
Date:   Tue Jun 11 12:24:23 2011 -0700
0

8 Answers 8

535
git reset --hard HEAD~1
git push -f <remote> <branch>

(Example push: git push -f origin bugfix/bug123)

This will undo the last commit and push the updated history to the remote. You need to pass the -f because you're replacing upstream history in the remote.

Edit:

Please note that --hard will make your commit unreachable (i.e. it will appear to be deleted, but you can still git show <hash> or git log <hash> it if you remember its hash). If you want to keep your changes, run:

git reset [--mixed] HEAD~1

At this point you have unstaged changes because you used --mixed, which is the default.

You may first want to update the remote tree first (i.e. remove the commit): git push -f <remote> <branch>

Since you still have your changes locally you can create another branch and commit them there (and push as you see fit).

18
  • 38
    Alternatively, use git reset --hard <the-sha-you-want-to-return-to>. Commented Jun 23, 2011 at 19:03
  • 2
    The reference is called HEAD (case sensitive)
    – dunni
    Commented Jun 23, 2011 at 19:36
  • 29
    Also, be careful - AFAIK you shouldn't be doing this if other people have pulled from the repo.
    – Amadan
    Commented Aug 15, 2013 at 0:44
  • 1
    @BipinVayalu It affects the branch you're currently on. More precisely, the HEAD. The HEAD is most often "attached" to a branch (pointing to a branch name instead of directly pointing to a commit). So, generally speaking, it will affect the branch HEAD points to. Use git log --decorate --oneline to find out where your HEAD points to. Commented Apr 29, 2014 at 9:51
  • 7
    git reset HEAD~1 if you don't want your changes to be gone(unstaged changes). Change, commit and push again git push -f [origin] [branch]
    – softvar
    Commented Jun 25, 2014 at 13:41
218

Generally, make an "inverse" commit, using:

git revert 364705c

then send it to the remote as usual:

git push

This won't delete the commit: it makes an additional commit that undoes whatever the first commit did. Anything else, not really safe, especially when the changes have already been propagated.

9
  • 10
    This is a safer (thus probably better) answer than Alexander Groß's (the chosen answer).
    – Graeck
    Commented Sep 13, 2013 at 23:32
  • 7
    @Graeck Every one of the solutions has its implications and merits. Commented Dec 13, 2013 at 13:55
  • 5
    This should be the accepted answer, it's best practice never to overwrite history, even more if collaborating with a team. git reset is only accepted if you still have not pushed the changes to the server. Commented Apr 24, 2014 at 19:53
  • 22
    @JosueIbarra I disagree for all cases. For most cases, yes, you should not overwrite history. However, I believe that there are legitimate cases where you absolutely should. For instance, you accidentally commit and push up your secrets file. That shouldn't be up in the git repo. So you can quickly remove it using the accepted answer here.
    – bfcoder
    Commented Apr 25, 2014 at 19:31
  • 14
    @bfcoder if you pushed a "secret" to a remote repo, it is no longer a secret. And the proper fix is to create a new secret, not to try to hide your mistake. Commented Dec 3, 2014 at 14:46
61

First of all, Relax.

"Nothing is under our control. Our control is mere illusion.", "To err is human"

I get that you've unintentionally pushed your code to remote-master. THIS is going to be alright.

1. At first, get the SHA-1 value of the commit you are trying to return, e.g. commit to master branch. run this:

git log

you'll see bunch of 'f650a9e398ad9ca606b25513bd4af9fe...' like strings along with each of the commits. copy that number from the commit that you want to return back.

2. Now, type in below command:

git reset --hard your_that_copied_string_but_without_quote_mark

you should see message like "HEAD is now at ". you are on clear. What it just have done is to reflect that change locally.

3. Now, type in below command:

git push -f

you should see like

"warning: push.default is unset; its implicit value has changed in..... ... Total 0 (delta 0), reused 0 (delta 0) ... ...your_branch_name -> master (forced update)."

Now, you are all clear. Check the master with "git log" again, your fixed_destination_commit should be on top of the list.

You are welcome (in advance ;))

UPDATE:

Now, the changes you had made before all these began, are now gone. If you want to bring those hard-works back again, it's possible. Thanks to git reflog, and git cherry-pick commands.

For that, i would suggest to please follow this blog or this post.

3
  • it is good measure to specify the remote and the branch too when doing "git push -f", but "git push -f" will work anyway most of the time
    – Robson
    Commented Jun 5, 2015 at 15:31
  • you could please condense and make a shorter reply...people wouldnt like reading a novel with unnecesary lines in a coding community
    – Ayan Mitra
    Commented Jun 8, 2022 at 15:08
  • 1
    I disagree @AyanMitra, I thought this was very helpful. Commented May 31 at 10:27
16

git reset HEAD~1 if you don't want your changes to be gone(unstaged changes). Change, commit and push again git push -f [origin] [branch]

1
  • 1
    This is most likely the answer you're looking for Commented Oct 12, 2022 at 11:47
7

Try using

git reset --hard <commit id> 

Please Note : Here commit id will the id of the commit you want to go to but not the id you want to reset. this was the only point where i also got stucked.

then push

git push -f <remote> <branch>
0
3

You can do an interactive rebase:

git rebase -i <commit>

This will bring up your default editor. Just delete the line containing the commit you want to remove to delete that commit.

You will, of course, need access to the remote repository to apply this change there too.

See this question: Git: removing selected commits from repository

3

Using git reset --hard HEAD~1, as most suggest, will undo the last commit locally, but you will also lose those local changes.

If you don't want to lose the local changes, and rather just undo last commit:

git reset HEAD~1

Then force push it to the origin to undo the commit in the remote (assuming <remote> == origin)

git push -f origin <branch>
2

Alternatively:

git push origin +364705c23011b0fc6a7ca2d80c86cef4a7c4db7ac8^:master

Force the master branch of the origin remote repository to the parent of last commit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.