How do I undo the most recent commits in Git?

18 560

6 134

I accidentally committed the wrong files to Git, but I haven't pushed the commit to the server yet.

How can I undo those commits from the local repository?

Hamza Yerlikaya

Posted 2009-05-29T18:09:14.627

Reputation: 25 731

133Warning: you should only do this if you have not yet pushed the commit to a remote, otherwise you will mess up the history of others who have already pulled the commit from the remote! – thSoft – 2015-05-13T21:18:57.297


Here's a very clear and thorough post about undoing things in git, straight from Github.

– Nobita – 2015-06-08T19:39:17.290

See this guide for Git commits undo on Local, Public and Git Branch How to undo Git Commits like pro

– Luzan Baral – 2017-02-27T03:53:19.727

41Before you post a new answer, consider there are already 65+ answers for this question. Make sure that your answer contributes what is not among existing answers. – Sazzad Hissain Khan – 2017-06-15T15:26:36.207

What @thSoft said is correct, pushing your commits and then git reset --bla foo requires you a git --force push [--all] [somewhere-else-than-origin]. Note that this is a "forced-push" which requires other people do a "forced-update". They should not use git pull as this implies a merge, but use git fetch [--all] and then git rebase origin/master (assume that the forced-update came from origin). All [bla] are optional parameters but sometimes needed, for example when you push to more than just one remote repository. – Roland – 2017-11-02T13:21:44.520

The reason for git fetch [--all] + git rebase origin/master is that this fixes your local history avoiding a merge making the history easier for GIT to handle. – Roland – 2017-11-02T13:23:23.723

15You know what git needs? git undo, that's it. Then the reputation git has for handling mistakes made by us mere mortals disappears. Implement by pushing the current state on a git stack before executing any git command. It would affect performance, so it would be best to add a config flag as to whether to enable it. – Yimin Rong – 2018-03-20T01:45:28.990

A nice explanation I have encountered:

– Guy Avraham – 2018-07-04T12:17:08.130


@YiminRong That can be done with Git's alias feature:

– Edric – 2018-10-05T14:50:08.673


20 066

Undo a commit and redo

$ git commit -m "Something terribly misguided"             # (1)
$ git reset HEAD~                                          # (2)
<< edit files as necessary >>                              # (3)
$ git add ...                                              # (4)
$ git commit -c ORIG_HEAD                                  # (5)
  1. This is what you want to undo
  2. This leaves your working tree (the state of your files on disk) unchanged but undoes the commit and leaves the changes you committed unstaged (so they'll appear as "Changes not staged for commit" in git status, and you'll need to add them again before committing). If you only want to add more changes to the previous commit, or change the commit message1, you could use git reset --soft HEAD~ instead, which is like git reset HEAD~ (where HEAD~ is the same as HEAD~1) but leaves your existing changes staged.
  3. Make corrections to working tree files.
  4. git add anything that you want to include in your new commit.
  5. Commit the changes, reusing the old commit message. reset copied the old head to .git/ORIG_HEAD; commit with -c ORIG_HEAD will open an editor, which initially contains the log message from the old commit and allows you to edit it. If you do not need to edit the message, you could use the -C option.

Beware however that if you have added any new changes to the index, using commit --amend will add them to your previous commit.

If the code is already pushed to your server and you have permissions to overwrite history (rebase) then:

git push origin master --force

You can also look at this answer:

How to move HEAD back to a previous location? (Detached head)

The above answer will show you git reflog which is used to find out what is the SHA-1 which you wish to revert to. Once you found the point to which you wish to undo to use the sequence of commands as explained above.

1 Note, however, that you don't need to reset to an earlier commit if you just made a mistake in your commit message. The easier option is to git reset (to upstage any changes you've made since) and then git commit --amend, which will open your default commit message editor pre-populated with the last commit message.

Esko Luontola

Posted 2009-05-29T18:09:14.627

Reputation: 65 716

145(Correction to what I wrote above; --mixed is the default. --mixed means to keep the changed files, but not keep them in the index. --soft would keep the changed files and keep them in the index as they were just before the changed commit. Sorry for the confusion.) – Ryan Lundy – 2011-11-17T02:40:53.077

4@madhairsilence, git actually keeps files around for about 30 days before garbage-collecting them, so with sufficient git-fu you can even undo a hard reset! OTOH, you should be keeping backup copies of your code elsewhere, because not even reflog can protect you from an accidental rm -rf or a hard drive crash. – DSimon – 2013-01-25T17:23:35.673

98zsh users might get: zsh: no matches found: HEAD^ - you need to escape ^ i.e. git reset --soft HEAD\^ – tnajdek – 2013-02-21T17:47:56.837

3This answer is confusing! Do the numbers represent alternatives or steps to do in order? I don't see how either could work. – Anko – 2013-06-27T08:22:53.533

4FYI: after stage 2, your changes are still staged. If you're trying to remove a specific file from your commit, you'll need to manually unstage it: git reset HEAD filename – aidan – 2013-07-31T05:53:35.227

3What happens if you don't commit to -c ORIG_HEAD, but just commit regularly? – Evan Carroll – 2014-03-05T02:53:15.443

I'm using gitbash and when I tried step 3 (i.e., $ edit), it outputs the following sh.exe": edit: command not found – Daniel – 2014-03-26T18:59:00.767

@Daniel "edit" means: use your favorite editor to make the changes you meant to make before the accidental commit. – Lambart – 2014-04-20T03:27:44.960

1just git reset HEAD^ will undo the previous commit and leave all the files there. – Alejandro Moreno – 2014-04-23T09:38:30.557

1The answer is not correct if, say by accident, git commit -a was issued when the -a should have been left out. In which case, it's better no leave out the --soft (which will result in --mixed which is the default) and then you can restage the changes you meant to commit. – dmansfield – 2014-07-02T21:19:30.687

1It's worth mentioning that git reset --soft HEAD~1 will put the changes from the undone commit into the staging area (index), even if there are other unstaged changes, thus being basically a completely perfect way of going back to just before git commit. – Roman Starkov – 2014-07-15T00:21:10.720

407And if the commit was to the wrong branch, you may git checkout theRightBranch with all the changes stages. As I just had to do. – Frank Shearar – 2010-10-05T15:44:20.307

4fatal: ambiguous argument 'HEAD~1': unknown revision or path not in the working tree. – Green – 2015-09-22T05:33:43.557

1Why not just use git commit --amend directly? It's way easier to remember and, to my knowledge, it works exactly the same way. – PLPeeters – 2015-12-15T11:33:23.133

1We really need to take that -a out of the last command. I think it overkill if the user is new to git. – Clutch – 2011-02-24T22:50:18.183

441If you're working in DOS, instead of git reset --soft HEAD^ you'll need to use git reset --soft HEAD~1. The ^ is a continuation character in DOS so it won't work properly. Also, --soft is the default, so you can omit it if you like and just say git reset HEAD~1. – Ryan Lundy – 2011-04-13T14:15:10.943

In more recent versions of git HEAD~ can be replaced with @~. – ThomasW – 2016-09-16T05:56:25.290

this solution is a problem if someone's accidentally pushes an incorrect commit? – BKSpurgeon – 2016-11-16T04:42:39.283

git add ... is not correct command, git add documentation never has a pattern for three dots, also I tried this command in my local, it does not work – IcyBrk – 2017-01-16T17:55:07.677

1@IcyBrk git add is a command. git add [--verbose | -v] [--dry-run | -n] [--force | -f] [--interactive | -i] [--patch | -p] [--edit | -e] [--[no-]all | --[no-]ignore-removal | [--update | -u]] [--intent-to-add | -N] [--refresh] [--ignore-errors] [--ignore-missing] [--chmod=(+|-)x] [--] [&lt;pathspec&gt;…​] – Ashraf.Shk786 – 2017-02-20T17:34:27.807

This doesn't really serve as way to undo a set of changes those? This is more if you need to amend a change? – jterm – 2017-03-14T21:30:44.510


If you have already pushed your changes to a remote branch, and you do git reset as shown above, you will be behind the remote branch. In such a situation, it is preferable to use git revert <commit hash> which will add another commit which reverts the previous changes. More information here

– user3613932 – 2017-04-13T18:08:08.423

@Green I think this happens if you only have 1 commit... there's no earlier commit to reset to. – Mr5o1 – 2017-09-13T23:05:49.737

It is almost a comprehensive answer. In case your 'last commit' === 'your first commit' — reset will do nothing but throw nice fatal message. In this case use git update-ref -d HEAD. – daGo – 2017-10-06T12:36:48.460

is there anyway to undo last 5 commits? would something like git reset HEAD~6 work? – Honey – 2017-11-10T18:18:07.830

Also if you have too-large files that don't belong and cant complete your initial commits. you can delete .git, remove your too large files. git init and commit -m 'initial commit' and then push -u origin master – microsaurus_dex – 2018-03-20T03:27:16.790

zsh users should turn off globbing with noglob git to get rid of this constant annoyance with caret ^ character. – Mr. Tao – 2018-05-06T10:40:42.917


  • git reset --hard HEAD~1, will go back to one commit and delete all file git knows about, but not untracked files, since git got no idea of them. - git reset HEAD~1, will keep all the changes of the current commit,but make them untracked - git reset --soft HEAD~1, will keep your commited files staged and untracked files still untracked
  • < – briefy – 2018-11-07T08:51:02.240

    19Also, in zsh you have to quote ^, so git reset --soft 'HEAD^'... at least I did – jberryman – 2011-10-27T18:24:16.450

    9 951

    Undoing a commit is a little scary if you don't know how it works. But it's actually amazingly easy if you do understand.

    Say you have this, where C is your HEAD and (F) is the state of your files.


    You want to nuke commit C and never see it again. You do this:

    git reset --hard HEAD~1

    The result is:


    Now B is the HEAD. Because you used --hard, your files are reset to their state at commit B.

    Ah, but suppose commit C wasn't a disaster, but just a bit off. You want to undo the commit but keep your changes for a bit of editing before you do a better commit. Starting again from here, with C as your HEAD:


    You can do this, leaving off the --hard:

    git reset HEAD~1

    In this case the result is:


    In both cases, HEAD is just a pointer to the latest commit. When you do a git reset HEAD~1, you tell Git to move the HEAD pointer back one commit. But (unless you use --hard) you leave your files as they were. So now git status shows the changes you had checked into C. You haven't lost a thing!

    For the lightest touch, you can even undo your commit but leave your files and your index:

    git reset --soft HEAD~1

    This not only leaves your files alone, it even leaves your index alone. When you do git status, you'll see that the same files are in the index as before. In fact, right after this command, you could do git commit and you'd be redoing the same commit you just had.

    One more thing: Suppose you destroy a commit as in the first example, but then discover you needed it after all? Tough luck, right?

    Nope, there's still a way to get it back. Type git reflog and you'll see a list of (partial) commit shas that you've moved around in. Find the commit you destroyed, and do this:

    git checkout -b someNewBranchName shaYouDestroyed

    You've now resurrected that commit. Commits don't actually get destroyed in Git for some 90 days, so you can usually go back and rescue one you didn't mean to get rid of.

    Ryan Lundy

    Posted 2009-05-29T18:09:14.627

    Reputation: 153 812

    1@Kyralessa: If I do git reset --hard HEAD^ twice, will the state shift to (A)? – dma_k – 2012-02-25T13:31:31.510

    35@dma_k, yes. Or you could do git reset --hard HEAD^^ once. I use the tilde (~) notation because the caret (^) notation doesn't work in DOS. – Ryan Lundy – 2012-02-25T15:02:44.487

    2I assume the 90 days thing is optional? – naught101 – 2012-06-14T03:20:36.747

    Doesn't work with OS x. - I get "ambiguous argument 'HEAD^' ... Unknow revision or path not in the working tree". Using he tilde version makes no difference. But git log and git status both appear to show there is a valid commit in place – Adam – 2012-06-22T10:53:40.273

    @Kyralessa - yes, I think that might be the problem. In this case, I committed something I thought was small - and when I tried to Push, I found it was 400 MB of data (!) (a deep nested Resources folder containing video and music files). So, I need to undo the commit, but keep the source files, and I'll commit them later, when I have better net connection - or to a different repo. – Adam – 2012-06-22T12:35:31.790

    16Another nice tip: You can re-attach the branch to the commit that you removed it from with git branch -f &lt;branch&gt; &lt;commit-id&gt;. Saves having to re-create commits! – naught101 – 2012-06-22T13:11:11.713

    182For a git beginner, it isn't obvious what the difference is between the last two options (--soft and the one above it). Mentioning the index doesn't help, we don't really know what that means yet. @nessur's connection between soft and Ctrl-Z really helped! But I still don't quite understand the difference between the two options. – Stomp – 2012-06-26T15:56:52.203

    116It's much better to be told 'why' something works, than just to be told the answer. Kudos to this description - it helped be 'get' git. – Chris Nash – 2012-07-03T19:13:35.470

    My thanks also for the explanation. Question about this statement though (re --hard) : "your files are reset to their state at commit B". Say I had only committed some of my modifications, other files being intended for future commits. How much am I resetting? Just the committed files? Or do I reset my whole working set? – TwainJ – 2012-10-30T06:09:49.733

    1Since so many people are supportive of the answer, I offer this feedback: write a quickly-readable and easily-executed set of instructions that I can run to fix my problem. The other answer has it; this one does not. – Danny – 2013-02-01T19:39:49.630

    3BEWARE! This might not do what you expect if your erroneous commit was a (fast-forward) merge! If your head is on a merge commit (ex: merged branch feature into master), git reset --hard~1 will point the master branch to the last commit inside the feature branch. In this case the specific commit ID should be used instead of the relative command. – Chris Kerekes – 2013-02-20T18:46:35.627

    Upon further thought this behaviour nay have been a result of merging the master branch into the feature branch first, testing and then (fast-forward) merging the feature branch into the master. – Chris Kerekes – 2013-02-21T14:05:28.407

    2Consider noting that the number in HEAD~1 can be substituted to any positive integer, e.g. HEAD~3. It may seem obvious, but beginners (like me) are very careful when running git commands, so they may not want to risk messing something up by testing this stuff themselves. – Šime Vidas – 2013-08-13T14:37:26.293

    Could someone comment on "what if you have already pushed the commit?". Is this answer still valid, or does that make it all more difficult? – GreenAsJade – 2013-09-05T03:10:54.640

    Great tip. Just used it, though I think it's relevant to note that any tags associated with the deleted commit will continue to exist, even though it won't appear in gitk. Delete it with "git tag -d XXXXX" where XXXXX is the tag name. – Phlucious – 2013-10-24T21:54:20.510

    54Missing a crucial point: If the said commit was previously 'pushed' to the remote, any 'undo' operation, no matter how simple, will cause enormous pain and suffering to the rest of the users who have this commit in their local copy, when they do a 'git pull' in the future.

    So, if the commit was already 'pushed', do this instead:

    git revert &lt;bad-commit-sha1-id&gt;
    git push origin :
     – FractalSpace  – 2013-11-08T23:43:18.513

    3@FractalSpace, it won't cause "enormous pain and suffering." I've done a few force pushes when using Git with a team. All it takes is communication. – Ryan Lundy – 2013-11-09T00:00:10.250

    5@Kyralessa In my workplace, messing up entire team's workflow and then telling them how to fix sh*t is not called 'communication'. git history re-write is a destructive operation that results in trashing of parts of the repo. Insisting on its use, while clear and safe alternatives are available is simply irresponsible. – FractalSpace – 2013-11-09T03:02:51.053

    @Kyralessa, I have a question about your answer. You say that "git reset --soft" "not only leaves your files alone, it even leaves your index alone. When you do git status, you'll see that the same files are in the index as before. In fact, right after this command, you could do git commit and you'd be redoing the same commit you just had." But if "git reset --soft" doesn't change the index, "git commit" wouldn't do anything. Do you mean "git commit -a" (assuming there had been no changes in the working directory beforehand) or am I missing something (likelier)? – Ellen Spertus – 2014-02-20T10:48:16.693

    2@espertus, perhaps one way to look at it is that there are three things working here: Your files, your index, and your history (that is, your branch pointer). Let's say you're at commit C (as above). Your files, index, and branch pointer match. If you use git reset --soft HEAD~1, your branch pointer moves back to B, but your files and index stay at their versions in C. If you use git reset --mixed HEAD~1, your branch pointer and index move back to B, but your files stay in their state at C. Then your files show changes but your index doesn't. git reset --hard HEAD~1 moves all three back. – Ryan Lundy – 2014-02-20T14:28:56.887

    @Kyralessa, thanks so much for the quick reply. When I woke up, I realized what I was missing. I assumed that the index is empty when everything is committed, but now I understand it's a copy. – Ellen Spertus – 2014-02-20T16:08:39.107


    Reset demystified by Scott Chacon (GitHub CIO, Pro Git author) explains and illustrates the git reset command and how to move the HEAD (--soft), update the index (--mixed, default) and update the working directory (--hard), from the bottom up.

    I've always used Kyralessa's answer as a cheat sheet, but Scott's blog post finally made it click and stick!

    – fspinnenhirn – 2014-08-19T03:13:04.547

    Adopted your illustrations with some improvements.

    – Nick Volynkin – 2015-07-06T11:09:09.880

    3I wanted to nuke a commit and never see it again. I used your example with --hard but what I didn't realise is that all my unstaged changes in my working tree also get nuked! I was going to commit these files as part of a later commit. Now it seems impossible to get these files back - I even tried the solution you posted about reflog but this didn't restore the previously-unstaged changes. – Kidburla – 2015-10-14T21:39:54.983

    link about index added in context, plus comment about backing up your entire project as a precaution @Kidburla. – matanster – 2015-10-15T17:10:06.110


    Hilariously true, the advice for local backup of git. Also, mandatory xkcd link illustrating the case

    – xDaizu – 2016-06-02T11:45:50.650

    In addition to the illustrations here, there are some intuitive graphical ones in this post

    – joelostblom – 2017-01-07T03:12:28.117

    By the way, @Alfe, that wasn't a typo. – Ryan Lundy – 2017-09-13T11:07:14.250

    @Kyralessa I see. But is it better to talk of "shas" than of "hashes"? The git man page mentions only hashes and "SHA-1 hashes". The question is rather: What improves the answer most? – Alfe – 2017-09-13T12:09:43.867

    1What is the difference between git reset --hard HEAD~1 and git reset --hard, if what you need is to simply get rid of all of the changes you made after your latest commit? I always use git reset --hard and it takes me back to my latest commit. For an analogy, I feel that it is kind of closing an application without saving changes so that everything that was on RAM memory is lost, but what you had on ROM memory is kept, using your latest commit as the ROM memory in this analogy, and your changes that have not been committed as stuff in the RAM memory that has not been saved yet. – Jaime Montoya – 2017-09-27T23:35:43.937

    Citing @poorva from comments in other posts below: 'To undo latest changes you can use git reset --hard , but if you have to hard remove last "n" commits you specify a SHA'. – Jaime Montoya – 2017-09-28T15:00:43.090

    Can you rearrange the post to make the --soft the first suggestion shown? One of our engineers didn't read the full article and used --hard on a shared working directory, which luckily only cost us one day of work. - Thanks.

    P.S. Yes, we know using shared working directories are bad practice, but this isn't storing code, but puppet configuration. – ruckc – 2017-10-24T20:32:28.707

    I'm trying git reset --soft HEAD~1 and I keep getting the following, why?: fatal: ambiguous argument 'HEAD~1': unknown revision or path not in the working tree. Use '--' to separate paths from revisions, like this: 'git <command> [<revision>...] -- [<file>...]' – gangelo – 2017-12-19T20:53:19.500

    @gangelo, how many commits do you have in your repository? More than one? It's hard to reset back before any commits, so I usually start a repo with a "dummy" commit (such as an empty .gitignore file) to get around this type of problem. – Ryan Lundy – 2017-12-19T21:05:53.800

    @Kyralessa I only have 1 commit but it's not pushed because, oddly enough, I need to pull down my .gitignore file from my repository. I guess I can commit and apply the .gitignore after the fact, but I don't remember how to do that and remember it being a hassle. I only want to undo the commit I have to a point where if I do 'git status' I'll see all my files tracked, and ready to commit as if it never happened. – gangelo – 2017-12-19T21:28:01.993

    @gangelo, have a look here for some ideas about how to revert that first commit:

    – Ryan Lundy – 2017-12-20T07:59:45.487

    1That last line that "Commits don't actually get destroyed in Git for some 90 days" is so awesome new info for me. – Ammar Mujeeb – 2018-08-08T06:10:16.800

    For me, the --hard flag was the right command. Thanks a lot for the explanations. I gave an upvote for it. – Battle Hawk – 2018-10-03T07:08:19.267


  • git reset --hard HEAD~1, will go back to one commit and delete all file git knows about, but not untracked files, since git got no idea of them.

  • git reset HEAD~1, will keep all the changes of the current commit,but make them untracked

  • git reset --soft HEAD~1, will keep your commited files staged and untracked files still untracked

  • < – briefy – 2018-11-07T04:27:47.700

    1 834

    This took me a while to figure out, so maybe this will help someone...

    There are two ways to "undo" your last commit, depending on whether or not you have already made your commit public (pushed to your remote repository):

    How to undo a local commit

    Let's say I committed locally, but now want to remove that commit.

    git log
        commit 101: bad commit    # latest commit, this would be called 'HEAD'
        commit 100: good commit   # second to last commit, this is the one we want

    To restore everything back to the way it was prior to the last commit, we need to reset to the commit before HEAD:

    git reset --soft HEAD^     # use --soft if you want to keep your changes
    git reset --hard HEAD^     # use --hard if you don't care about keeping the changes you made

    Now git log will show that our last commit has been removed.

    How to undo a public commit

    If you have already made your commits public, you will want to create a new commit which will "revert" the changes you made in your previous commit (current HEAD).

    git revert HEAD

    Your changes will now be reverted and ready for you to commit:

    git commit -m 'restoring the file I removed by accident'
    git log
        commit 102: restoring the file I removed by accident
        commit 101: removing a file we don't need
        commit 100: adding a file that we need

    For more info, check out Git Basics - Undoing Things


    Posted 2009-05-29T18:09:14.627

    Reputation: 95 536

    76I found this answer the clearest. git revert HEAD^ is not the previous, is the previous of the previous. I did : git revert HEAD and then push again and it worked :) – nacho4d – 2011-07-14T08:32:53.040

    This is the best answer because it does not mess with your git history – Lukas – 2018-08-03T10:38:54.913

    1 649

    Add/remove files to get things the way you want:

    git rm classdir
    git add sourcedir

    Then amend the commit:

    git commit --amend

    The previous, erroneous commit will be edited to reflect the new index state - in other words, it'll be like you never made the mistake in the first place.

    Note that you should only do this if you haven't pushed yet. If you have pushed, then you'll just have to commit a fix normally.


    Posted 2009-05-29T18:09:14.627

    Reputation: 172 686

    13@Dennis, git commit --amend turns the current tree (ie, staged changes) into a commit, overwriting current HEAD. After that point, they're not considered staged anymore because they're part of the commit (ie, git diff --cached is blank), but they're not "removed" or "lost". – bdonlan – 2012-02-01T03:08:06.567

    20Does this work when I did a git commit --amend and what I really meant to do is a git commit? – dbm – 2011-05-18T13:07:31.800

    62@dbm, if you accidentally amended, use git reset --soft &lt;oldref&gt;, where oldref is the commit ID before the amend. You can use git reflog to identify the old commit ID. This will undo the effects of the amend, but leave changes staged. Then just do git commit to commit as a regular commit. – bdonlan – 2011-05-18T14:20:48.560


    git rm yourfiles/*.class
    git commit -a -m "deleted all class files in folder 'yourfiles'"


    git reset --hard HEAD~1

    Warning: The above command will permanently remove the modifications to the .java files (and any other files) that you wanted to commit.

    The hard reset to HEAD-1 will set your working copy to the state of the commit before your wrong commit.

    Lennart Koopmann

    Posted 2009-05-29T18:09:14.627

    Reputation: 14 557

    11git commit -a -m "" or git commit -am "" naturally! :] – trejder – 2014-06-21T16:31:59.657

    Another 'shortcut' use of stash; if you want to unstage everything (undo git add), just git stash, then git stash pop – seanriordan08 – 2015-12-08T22:30:29.923

    22You can "git stash save" working copy changes, do a hard reset and then "git stash pop" to get them back, though I suppose a soft reset would be simpler. – Asad R. – 2011-04-15T13:33:03.347

    69"--hard" will get rid of the modified .java files in the working directory that he wanted to commit. – Esko Luontola – 2009-05-29T18:26:59.053


    To change the last commit

    Replace the files in the index:

    git rm --cached *.class
    git add *.java

    Then, if it's a private branch, amend the commit:

    git commit --amend

    Or, if it's a shared branch, make a new commit:

    git commit -m 'Replace .class files with .java files'

    (to change a previous commit, use the awesome interactive rebase)

    ProTip™:   Add *.class to a gitignore to stop this happening again.

    To revert a commit

    Amending a commit is the ideal solution if you need to change the last commit, but a more general solution is reset.

    You can reset git to any commit with:

    git reset @~N

    Where N is the number of commits before HEAD, and @~ resets to the previous commit.

    So, instead of amending the commit, you could use:

    git reset @~
    git add *.java
    git commit -m "Add .java files"

    Check out git help reset, specifically the sections on --soft --mixed and --hard, for a better understanding of what this does.


    If you mess up, you can always use the reflog to find dropped commits:

    $ git reset @~
    $ git reflog
    c4f708b [email protected]{0}: reset: moving to @~
    2c52489 [email protected]{1}: commit: added some .class files
    $ git reset 2c52489
    ... and you're back where you started


    Posted 2009-05-29T18:09:14.627

    Reputation: 24 860

    For those reading in future - please note that git revert is a separate command - which basically 'resets' a single commimt. – BKSpurgeon – 2018-08-08T07:11:00.777


    Use git revert <commit-id>

    To get the commit ID, just use git log

    Jaco Pretorius

    Posted 2009-05-29T18:09:14.627

    Reputation: 20 238

    25If you committed to the wrong branch: once reverted, switch to the correct branch and cherry-pick the commit. – Kris – 2012-06-27T11:02:58.000

    10What does that mean, cherry pick the commit? In my case, I was on the wrong branch when I edited a file. I committed it then realized I was in the wrong branch. Using "git reset --soft HEAD~1" got me back to just before the commit, but now if I checkout the correct branch, how do I undo the changes to the file in wrong branch but instead make them (in the same named file) in the correct branch? – astronomerdave – 2015-01-13T22:05:57.217

    I just utilized git revert commit-id worked like a charm. Of course then you will need to push your changes. – Casey Robinson – 2016-01-25T21:07:16.507

    5I believe that would be git cherry-pick &lt;&lt;erroneous-commit-sha&gt;&gt; @astronomerdave. From, Mr. Almost-2-Years-Late-to-the-Party. – Tom Howard – 2016-10-20T18:19:50.407

    @Kris: Instead of cherry-pick use rebase. Because it is advanced cherry-picking – Eugen Konkov – 2018-11-10T09:38:58.443


    If you are planning to undo a local commit entirely, whatever you change you did on the commit, and if you don't worry anything about that, just do the following command.

    git reset --hard HEAD^1

    (This command will ignore your entire commit and your changes will be lost completely from your local working tree). If you want to undo your commit, but you want your changes in the staging area (before commit just like after git add) then do the following command.

    git reset --soft HEAD^1

    Now your committed files come into the staging area. Suppose if you want to upstage the files, because you need to edit some wrong content, then do the following command

    git reset HEAD

    Now committed files to come from the staged area into the unstaged area. Now files are ready to edit, so whatever you change, you want to go edit and added it and make a fresh/new commit.


    Madhan Ayyasamy

    Posted 2009-05-29T18:09:14.627

    Reputation: 10 081

    9@SMR, In your example, all are pointing into current HEAD only. HEAD^ = HEAD^1. As well as HEAD^1 = HEAD~1. When you use HEAD~2, there is a difference between ~ and ^ symbols. If you use ~2 means “the first parent of the first parent,” or “the grandparent”. – Madhan Ayyasamy – 2015-12-14T15:34:11.460


    If you have Git Extras installed, you can run git undo to undo the latest commit. git undo 3 will undo the last 3 commits.


    Posted 2009-05-29T18:09:14.627

    Reputation: 367 419


    I wanted to undo the lastest 5 commits in our shared repository. I looked up the revision id that I wanted to rollback to. Then I typed in the following.

    prompt> git reset --hard 5a7404742c85
    HEAD is now at 5a74047 Added one more page to catalogue
    prompt> git push origin master --force
    Total 0 (delta 0), reused 0 (delta 0)
    remote: bb/acl: neoneye is allowed. accepted payload.
    To [email protected]:thecompany/prometheus.git
     + 09a6480...5a74047 master -> master (forced update)


    Posted 2009-05-29T18:09:14.627

    Reputation: 30 107

    16Rewriting history on a shared repository is generally a very bad idea. I assume you know what you're doing, I just hope future readers do too. – Brad Koch – 2012-12-07T16:02:12.403

    Yes rollback is dangerous. Make sure that your working copy is in the desired state before you push. When pushing then the unwanted commits gets deleted permanently. – neoneye – 2012-12-08T14:14:43.680


    "Just like in the real world, if you want to rewrite history, you need a conspiracy: everybody has to be 'in' on the conspiracy (at least everybody who knows about the history, i.e. everybody who has ever pulled from the branch)." Source:

    – Mikko Rantalainen – 2013-08-07T10:10:25.843

    fantastic. This worked like a charm. There should be a git option for independently working developers - where one can use GitX, or a delete button next to a commit, and a confirmation - so this process is not so enigmatic :) – zero_cool – 2014-06-20T15:31:54.717


    I prefer to use git rebase -i for this job, because a nice list pops up where I can choose the commits to get rid of. It might not be as direct as some other answers here, but it just feels right.

    Choose how many commits you want to list, then invoke like this (to enlist last three)

    git rebase -i HEAD~3

    Sample list

    pick aa28ba7 Sanity check for RtmpSrv port
    pick c26c541 RtmpSrv version option
    pick 58d6909 Better URL decoding support

    Then git will remove commits for any line that you remove.

    Steven Penny

    Posted 2009-05-29T18:09:14.627

    Reputation: 1


    How to fix the previous local commit

    Use git-gui (or similar) to perform a git commit --amend. From the GUI you can add or remove individual files from the commit. You can also modify the commit message.

    How to undo the previous local commit

    Just reset your branch to the previous location (for example, using gitk or git rebase). Then reapply your changes from a saved copy. After garbage collection in your local repository, it will be like the unwanted commit never happened. To do all of that in a single command, use git reset HEAD~1.

    Word of warning: Careless use of git reset is a good way to get your working copy into a confusing state. I recommend that Git novices avoid this if they can.

    How to undo a public commit

    Perform a reverse cherry pick (git-revert) to undo the changes.

    If you haven't yet pulled other changes onto your branch, you can simply do...

    git revert --no-edit HEAD

    Then push your updated branch to the shared repository.

    The commit history will show both commits, separately.

    Advanced: Correction of private branch in public repository

    This can be dangerous -- be sure you have a local copy of the branch to repush.

    Also note: You don't want to do this if someone else may be working on the branch.

    git push --delete (branch_name) ## remove public version of branch

    Cleanup your branch locally then repush...

    git push origin (branch_name)

    In the normal case, you probably needn't worry about your private-branch commit history being pristine. Just push a followup commit (see 'How to undo a public commit' above), and later, do a squash-merge to hide the history.


    Posted 2009-05-29T18:09:14.627

    Reputation: 25 021

    5gitk --all $(git reflog | cut -c1-7)&amp; may be helpful for finding the previous revision if you want to undo an '--amend' commit. – nobar – 2014-10-18T23:38:11.210

    3It should be noted that if you're attempting to remove secret information before pushing to a shared repository, doing a revert won't help you, because the information will still be in the history in the previous commit. If you want to ensure the change is never visible to others you need to use git reset – Jherico – 2015-09-04T04:52:01.620

    See also:

    – nobar – 2016-04-05T04:20:04.973

    I think 'private'/'public' would more correctly be 'local'/'remote'. – nobar – 2018-03-28T14:59:27.083

    Correcting a private branch in remote repository can also be done by simply git push origin (branch_name) --force – nobar – 2018-09-07T12:09:38.767


    If you have committed junk but not pushed,

    git reset --soft HEAD~1

    HEAD~1 is a shorthand for the commit before head. Alternatively you can refer to the SHA-1 of the hash if you want to reset to. --soft option will delete the commit but it will leave all your changed files "Changes to be committed", as git status would put it.

    If you want to get rid of any changes to tracked files in the working tree since the commit before head use "--hard" instead.


    If you already pushed and someone pulled which is usually my case, you can't use git reset. You can however do a git revert,

    git revert HEAD

    This will create a new commit that reverses everything introduced by the accidental commit.


    Posted 2009-05-29T18:09:14.627

    Reputation: 22

    I'm in the 2nd case, but when I do "git revert HEAD" it says "error: Commit [ID] is a merge but no -m option was given. fatal: revert failed". Any suggestions? – metaforge – 2014-11-12T19:36:02.793

    1Probably worth mentioning that instead of HEAD~1 you could use the actual hash as displayed by git log --stat or by git reflog - useful when you need to 'undo' more than one commit. – ccpizza – 2014-12-07T00:38:49.550


    If you want to permanently undo it and you have cloned some repository

    The commit id can be seen by

    git log 

    Then you can do -

    git reset --hard <commit_id>
    git push origin <branch_name> -f


    Posted 2009-05-29T18:09:14.627

    Reputation: 958

    1Should be marked as answer, because you can specify which SHA you want. – Belfield – 2017-07-28T10:49:14.133

    What if you do not use "<commit_id>" and simply use "git reset --hard"? I typically just want to get rid of my latest updates that I have not committed yet and got back to the latest commit I made, and I always use "git reset --hard". – Jaime Montoya – 2017-09-27T23:30:18.703

    1@JaimeMontoya To undo latest changes you can use git reset --hard , but if you have to hard remove last "n" commits you specify a SHA – poorva – 2017-09-28T13:10:31.400


    On SourceTree (GUI for GitHub), you may right-click the commit and do a 'Reverse Commit'. This should undo your changes.

    On the terminal:

    You may alternatively use:

    git revert


    git reset --soft HEAD^ # Use --soft if you want to keep your changes.
    git reset --hard HEAD^ # Use --hard if you don't care about keeping your changes.

    Varun Parakh

    Posted 2009-05-29T18:09:14.627

    Reputation: 8


    A single command:

    git reset --soft 'HEAD^' 

    It works great to undo the last local commit!

    Manish Shrivastava

    Posted 2009-05-29T18:09:14.627

    Reputation: 17 818

    8I needed to write git reset --soft "HEAD^" with double quotes, because I write it from Windows command prompt. – Ena – 2014-04-23T09:13:35.283

    1It should work without any quotes. – jkulak – 2016-11-10T19:19:44.937


    Just reset it doing the command below using git:

    git reset --soft HEAD~1

    Explain: what git reset does, it's basically reset to any commit you'd like to go back to, then if you combine it with --soft key, it will go back, but keep the changes in your file(s), so you get back to the stage which the file was just added, HEAD is the head of the branch and if you combine with ~1 (in this case you also use HEAD^), it will go back only one commit which what you want...

    I create the steps in the image below in more details for you, including all steps that may happens in real situations and committing the code:

    How to undo the last commits in Git?


    Posted 2009-05-29T18:09:14.627

    Reputation: 43 514


    How to undo the last Git commit?

    To restore everything back to the way it was prior to the last commit, we need to reset to the commit before HEAD.

    1. If you don't want to keep your changes that you made:

      git reset --hard HEAD^
    2. If you want to keep your changes:

      git reset --soft HEAD^

    Now check your git log. It will show that our last commit has been removed.

    Ranjithkumar Ravi

    Posted 2009-05-29T18:09:14.627

    Reputation: 2 359


    Use reflog to find a correct state

    git reflog

    reflog before REFLOG BEFORE RESET

    Select the correct reflog (f3cb6e2 in my case) and type

    git reset --hard f3cb6e2

    After that the repo HEAD will be reset to that HEADid reset effect LOG AFTER RESET

    Finally the reflog looks like the picture below

    reflog after REFLOG FINAL

    Shubham Chaudhary

    Posted 2009-05-29T18:09:14.627

    Reputation: 12 713


    "Reset the working tree to the last commit"

    git reset --hard HEAD^ 

    "Clean unknown files from the working tree"

    git clean    

    see - Git Quick Reference

    NOTE: This command will delete your previous commit, so use with caution! git reset --hard is safer –


    Posted 2009-05-29T18:09:14.627

    Reputation: 10 651


    First run:

    git reflog

    It will show you all the possible actions you have performed on your repository, for example, commit, merge, pull, etc.

    Then do:

    git reset --hard ActionIdFromRefLog

    U. Ali

    Posted 2009-05-29T18:09:14.627

    Reputation: 71


    Another way:

    Checkout the branch you want to revert, then reset your local working copy back to the commit that you want to be the latest one on the remote server (everything after it will go bye-bye). To do this, in SourceTree I right-clicked on the and selected "Reset BRANCHNAME to this commit".

    Then navigate to your repository's local directory and run this command:

    git -c diff.mnemonicprefix=false -c core.quotepath=false push -v -f --tags REPOSITORY_NAME BRANCHNAME:BRANCHNAME

    This will erase all commits after the current one in your local repository but only for that one branch.


    Posted 2009-05-29T18:09:14.627

    Reputation: 4 874


    Undo last commit:

    git reset --soft HEAD^ or git reset --soft HEAD~

    This will undo the last commit.

    Here --soft means reset into staging.

    HEAD~ or HEAD^ means to move to commit before HEAD.

    Replace last commit to new commit:

    git commit --amend -m "message"

    It will replace the last commit with the new commit.


    Posted 2009-05-29T18:09:14.627

    Reputation: 803


    Type git log and find the last commit hash code and then enter:

    git reset <the previous co>


    Posted 2009-05-29T18:09:14.627

    Reputation: 31


    In my case I accidentally committed some files I did not want to. So I did the following and it worked:

    git reset --soft HEAD^
    git rm --cached [files you do not need]
    git add [files you need]
    git commit -c ORIG_HEAD

    Verify the results with gitk or git log --stat


    Posted 2009-05-29T18:09:14.627

    Reputation: 359


    Use SourceTree (graphical tool for Git) to see your commits and tree. You can manually reset it directly by right clicking it.

    iOS Coder

    Posted 2009-05-29T18:09:14.627

    Reputation: 19


    There are two main scenarios

    You haven't pushed the commit yet

    If the problem was extra files you commited (and you don't want those on repository), you can remove them using git rm and then commiting with --amend

    git rm <pathToFile>

    You can also remove entire directories with -r, or even combine with other Bash commands

    git rm -r <pathToDirectory>
    git rm $(find -name '*.class')

    After removing the files, you can commit, with --amend option

    git commit --amend -C HEAD # the -C option is to use the same commit message

    This will rewrite your recent local commit removing the extra files, so, these files will never be sent on push and also will be removed from your local .git repository by GC.

    You already pushed the commit

    You can apply the same solution of the other scenario and then doing git push with the -f option, but it is not recommended since it overwrites the remote history with a divergent change (it can mess your repository).

    Instead, you have to do the commit without --amend (remember this about -amend`: That option rewrites the history on the last commit).


    Posted 2009-05-29T18:09:14.627

    Reputation: 9 522


    To reset to the previous revision, permanently deleting all uncommitted changes:

    git reset --hard HEAD~1


    Posted 2009-05-29T18:09:14.627

    Reputation: 2 380

    14Maybe you could at a note/warning that his command will throw away the commit and the changes in the working directory without asking any further. – cr7pt0gr4ph7 – 2014-11-24T22:35:29.280

    13Use --soft to keep your changes as uncommitted changes, --hard to nuke the commit completely and revert back by one. Remember to do such operations only on changes, that are not pushed yet. – Yunus Nedim Mehel – 2015-03-09T09:11:06.323

    @Zaz: You are right; maybe I should have clarified that. Only files/changes that have been either added to index (/staged) or have been committed can possibly be recovered. Uncommitted, unstaged changes are, as you said, completely thrown away by git reset --hard. – cr7pt0gr4ph7 – 2016-09-13T21:17:10.430

    1As a sidenote: Everytime a file is staged, git stores its contents in its object database. The stored contents are only removed when garbage collection is executed. It is therefore possible to recover the last staged version of a file that was not currently staged when git reset --hard was executed (see the posts linked above for more information). – cr7pt0gr4ph7 – 2016-09-13T21:22:52.620


    There are many ways to do it:

    Git command to undo the last commit/ previous commits:

    Warning: Do Not use --hard if you do not know what you are doing. --hard is too dangerous, and it might delete your files.

    Basic command to revert the commit in Git is:

    $ git reset --hard <COMMIT -ID>


    $ git reset --hard HEAD~<n>

    COMMIT-ID: ID for the commit

    n: is number of last commits you want to revert

    You can get the commit id as shown below:

    $ **git log --oneline**
    d81d3f1 function to subtract two numbers
    be20eb8 function to add two numbers
    bedgfgg function to mulitply two numbers

    where d81d3f1 and be20eb8 are commit id.

    Now let's see some cases:

    Suppose you want to revert the last commit 'd81d3f1'. Here are two options:

    $ git reset --hard d81d3f1


    $ git reset --hard HEAD~1

    Suppose you want to revert the commit 'be20eb8':

    $ git reset --hard be20eb8

    For more detailed information you can refer and try out some other commands too for resetting head to a specified state:

    $ git reset --help


    Posted 2009-05-29T18:09:14.627

    Reputation: 347


    Simple, run this in your command line:

    git reset --soft HEAD~ 


    Posted 2009-05-29T18:09:14.627

    Reputation: 17 529


    For a local commit

    git reset --soft HEAD~1

    or if you do not remember exactly in which commit it is, you might use

    git rm --cached <file>

    For a pushed commit

    The proper way of removing files from the repository history is using git filter-branch. That is,

    git filter-branch --index-filter 'git rm --cached <file>' HEAD

    But I recomnend you use this command with care. Read more at git-filter-branch(1) Manual Page.


    Posted 2009-05-29T18:09:14.627

    Reputation: 1 141


    You can use:

    git reset [email protected]{1}

    This command will delete your wrong commit without a Git log.

    Min Han

    Posted 2009-05-29T18:09:14.627

    Reputation: 303


    WHAT TO USE, reset --soft or reset --hard?

    I am just adding two cents for @Kyralessa's answer:

    If you are unsure what to use go for --soft (I used this convention to remember it --soft for safe).

    Why ?

    If you choose --hard by mistake you will LOSE your changes as it wasn't before. If you choose --soft by mistake you can achieve the same results of --hard by applying additional commands

    git reset HEAD file.html
    git checkout -- file.html

    Full example

    echo "some changes..." > file.html
    git add file.html
    git commit -m "wrong commit"
    # I need to reset
    git reset --hard HEAD~1 (cancel changes)
    # OR
    git reset --soft HEAD~1 # Back to staging
    git reset HEAD file.html # back to working directory
    git checkout -- file.html # cancel changes

    Credits goes to @Kyralessa.


    Posted 2009-05-29T18:09:14.627

    Reputation: 13 521


    Usually, you want to undo a commit because you made a mistake and you want to fix it - essentially what the OP did when he asked the question. So really, you actually want to redo a commit.

    Most of the answers here focus on the command line. While the command line is the best way to use Git when you're comfortable with it, its probably a bit alien to those coming from other version control systems to Git.

    Here's how to do it using a GUI. If you have Git installed, you already have everything you need to follow these instructions.

    NOTE: I will assume here that you realised the commit was wrong before you pushed it. If you don't know what pushing is, you probably haven't pushed, so carry on with the instructions. If you have pushed the faulty commit, the least risky way is just to follow up the faulty commit with a new commit that fixes things, the way you would do it in a version control system that does not allow you to rewrite history.

    That said, here's how to fix your most recent fault commit using a GUI:

    1. Navigate to your repository on the command line and start the gui with git gui
    2. Choose "Amend last commit". You will see your last commit message, the files you staged and the files you didn't.
    3. Now change things to how you want them to look and click Commit.


    Posted 2009-05-29T18:09:14.627

    Reputation: 30 210


    If you are working with SourceTree, this will help you.

    Right click on the commit then select "Reset (current branch)/master to this commit" and last select "Soft" reset.

    Enter image description here


    Posted 2009-05-29T18:09:14.627

    Reputation: 2 682


    Think we have code.txt file. We make some changes on it and commit. We can undo this commit in three ways, but first you should know what is the staged file... An staged file is a file that ready to commit and if you run git status this file will be shown with green color and if this is not staged for commit will be shown with red color:

    enter image description here

    It means if you commit your change, your changes on this file is not saved. You can add this file in your stage with git add code.txt and then commit your change:

    enter image description here

    Undo last commit:

    1. Now if we want just undo commit without any other changes, we can use

      git reset --soft HEAD^

      enter image description here

    2. If we want undo commit and its changes (THIS IS DANGEROUS, because your change will lost), we can use

      git reset --hard HEAD^

      enter image description here

    3. And if we want undo commit and remove changes from stage, we can use

      git reset --mixed HEAD^ or in a short form git reset HEAD^

      enter image description here

    Ali Motameni

    Posted 2009-05-29T18:09:14.627

    Reputation: 532


    Just undo the last commit:

    git reset --soft HEAD~

    Or undo the time before last time commit:

    git reset --soft HEAD~2

    Or undo any previous commit:

    git reset --soft <commitID>

    (you can get the commitID using git reflog)

    When you undo a previous commit, remember to clean the workplace with

    git clean

    More details can be found in the docs: git-reset


    Posted 2009-05-29T18:09:14.627

    Reputation: 373


    To undo your local commit you use git reset <commit>. Also that tutorial is very helpful to show you how it works.

    Alternatively, you can use git revert <commit>: reverting should be used when you want to add another commit that rolls back the changes (but keeps them in the project history).


    Posted 2009-05-29T18:09:14.627

    Reputation: 1 325


    A Typical Git Cycle

    In speaking of Git-related commands in the previous answers, I would like to share my typical Git cycles with all readers which may helpful. Here is how I work with Git,

    1. Cloning the first time from the remote server

      git clone $project

    2. Pulling from remote (when I don't have a pending local commit to push)

      git pull

    3. Adding a new local file1 into $to_be_committed_list (just imagine $to_be_committed_list means staged area)

      git add $file1

    4. Removing mistakenly added file2 from $to_be_committed_list (assume that file2 is added like step 3, which I didn't want)

      git reset $file2

    5. Committing file1 which is in $to_be_committed_list

      git commit -m "commit message description"

    6. Syncing local commit with remote repository before pushing

      git pull --rebase

    7. Resolving when conflict occurs prerequisite configure mergetool

      git mergetool #resolve merging here, also can manually merge

    8. Adding conflict-resolved files, let's say file1:

      git add $file1

    9. Continuing my previous rebase command

      git rebase --continue

    10. Pushing ready and already synced last local commit

      git push origin head:refs/for/$branch # branch = master, dev, etc.

    Sazzad Hissain Khan

    Posted 2009-05-29T18:09:14.627

    Reputation: 14 113


    Undo the Last Commit

    There are tons of situations where you really want to undo that last commit into your code. E.g. because you'd like to restructure it extensively - or even discard it altogether!

    In these cases, the "reset" command is your best friend:

    $ git reset --soft HEAD~1

    The above command (reset) will rewind your current HEAD branch to the specified revision. In our example above, we'd like to return to the one before the current revision - effectively making our last commit undone.

    Note the --soft flag: this makes sure that the changes in undone revisions are preserved. After running the command, you'll find the changes as uncommitted local modifications in your working copy.

    If you don't want to keep these changes, simply use the --hard flag. Be sure to only do this when you're sure you don't need these changes any more.

    $ git reset --hard HEAD~1

    Enter image description here


    Posted 2009-05-29T18:09:14.627

    Reputation: 576


    VISUAL STUDIO USERS (2015, etc.)

    If you cannot synchronise in Visual Studio as you are not allowed to push to a branch like "development" then as much as I tried, in Visual Studio NEITHER the REVERT NOR the RESET (hard or soft) would work.

    Per the answer with TONS OF VOTES:

    Use this at the command prompt of root of your project to nuke anything that will attempt to get pushed:

    git reset --hard HEAD~1

    Backup or zip your files just in case you don't wish to lose any work, etc...

    Tom Stickel

    Posted 2009-05-29T18:09:14.627

    Reputation: 12 064


    I got the commit ID from bitbucket and then did:

    git checkout commitID .


    git checkout 7991072 .

    And it reverted it back up to that working copy of that commit.


    Posted 2009-05-29T18:09:14.627

    Reputation: 1 304


    Suppose you made a wrong commit locally and pushed it to a remote repository. You can undo the mess with these two commands:

    First, we need to correct our local repository by going back to the commit that we desire:

    git reset --hard <previous good commit id where you want the local repository  to go>

    Now we forcefully push this good commit on the remote repository by using this command:

    git push --force-with-lease

    The the 'with-lease' version of the force option it will prevent accidental deletion of new commits you do not know about (i.e. coming from another source since your last pull).


    Posted 2009-05-29T18:09:14.627

    Reputation: 3 759


    You need to do the easy and fast

        git commit --amend

    if it's a private branch or

        git commit -m 'Replace .class files with .java files'

    if it's a shared or public branch.

    Yahs Hef

    Posted 2009-05-29T18:09:14.627

    Reputation: 693


    In my case I committed and pushed to the wrong branch, so what I wanted was to have all my changes back so I can commit them to a new correct branch, so I did this:

    On the same branch that you committed and pushed, if you type "git status" you wont't see anything new because you committed and pushed, now type:

    git reset --soft HEAD~1

    This will get all your changes(files) back in the stage area, now to get them back in the working directory(unstage) you just type:

    git reset FILE

    Where "File" is the file that you want to commit again. Now this FILE should be in the working directory(unstaged) with all the changes that you did. Now you can change to whatever branch that you want and commit the changes in that branch. Hope this helps other people that made the same mistake I did. Of course the initial branch that you committed is still there with all changes, but in my case that was ok, if it is not for you you can look for ways to revert that commit in that branch.


    Posted 2009-05-29T18:09:14.627

    Reputation: 256


    You have several options to undo your last commit. Here are some of your options summarize into a single answer with code snippets

    First of all you need to figure which are the "wrong" commits which you wish to discard. We will use git reflog to find it.

    git reflog

    You can always use the reflog as well.
    git reflog will display any change which updated the HEAD and checking out the desired reflog entry will set the HEAD back to this commit.

    Every time the HEAD is modified there will be a new entry in the reflog.
    The reflog is similar to unix history command and is kept locally on your machine.

    git reflog
    git checkout [email protected]{...}
    # or
    git checkout <sha-1>

    Using the checkout you can go back to any desired commit and you can create branch or any other options that git checkout will allow you to do.

    enter image description here

    git reset HEAD --hard <commit_id>

    "Move" your head back to the desired commit.
    Git reset will checkout the desired commit content to your staging area and/or to your working directory based upon the value --hard/--soft/--mixed you choose to pick. --hard will update both stage area and working directory with the given content and will "detach" any other commits beyond this point on your local branch.

    If those commits are not part of any other branch they will become "dangle".
    "dangle" content means that there is a un-reachable content in your local repository which is not part of any other branch and can be removed or will be removed by the gc.

    # This will destroy any local modifications.
    # Don't do it if you have uncommitted work you want to keep.
    git reset --hard 0d1d7fc32
    # Alternatively, if there's work to keep:
    git stash
    git reset --hard 0d1d7fc32
    git stash pop
    # This saves the modifications, then reapplies that patch after resetting.
    # You could get merge conflicts, if you've modified things which were
    # changed since the commit you reset to.

    This schema illustrate which command does what.
    As you can see there reset && checkout modify the HEAD.

    enter image description here


    Posted 2009-05-29T18:09:14.627

    Reputation: 49 194


    Use this command

    git checkout -b old-state 0d1d7fc32

    Jishnu Sukumaran

    Posted 2009-05-29T18:09:14.627

    Reputation: 46


    Use this command:

    git checkout -b old-state number_commit


    Posted 2009-05-29T18:09:14.627

    Reputation: 638


    Remove a wrong commit that is already pushed to Github

    git push origin +(previous good commit id):(branch name)

    Please specify the last good commit id you would like to reset back in Github.

    For example. If latest commit id is wrong then specify the previous commit id in above git command with branch name.

    You can get previous commit id using git log

    V V

    Posted 2009-05-29T18:09:14.627

    Reputation: 268


    You can always do a git checkout <SHA code> of the previous version and then commit again with the new code.

    shreshta bm

    Posted 2009-05-29T18:09:14.627

    Reputation: 156


    In order to get rid of (all the changes in) last commit, last 2 commits and last n commits:

    git reset --hard HEAD~1
    git reset --hard HEAD~2
    git reset --hard HEAD~n

    And, to get rid of anything after a specific commit:

    git reset --hard <commit sha>


    git reset --hard 0d12345

    1- Be careful, because of "hard" option, it deletes the local changes in your repo as well and reverts to the previous mentioned commit. You should run this if you are sure you messed up in your last commit(s) and would like to go back in time.

    2- Usually 7 letters of "commit sha" is enough, but in bigger projects, you may need up to 12 letters to guarantee be unique. You also can mention the whole 40 letters sha.

    3- The above commands work in Github for Windows as well.


    Posted 2009-05-29T18:09:14.627

    Reputation: 1 381


    Undo last commit:

    git reset --soft HEAD^ or git reset --soft HEAD~

    This will undo the last commit.

    Here --soft means reset into staging.

    HEAD~ or HEAD^ means to move to commit before HEAD.

    Replace last commit to new commit:

    git commit --amend -m "message"

    It will replace the last commit with the new commit.

    ankit patidar

    Posted 2009-05-29T18:09:14.627

    Reputation: 1 841


    Reference: How to undo last commit in Git?

    If you have Git Extensions installed you can easily undo/revert any commit (you can download git extensions from here).

    Open Git Extensions, right click on the commit you want to revert then select "Revert commit".

    Git Extensions screen shot

    A popup will be opened (see the screenshot below)

    Revert commit popup

    Select "Automatically create a commit" if you want to directly commit the reverted changes or if you want to manually commit the reverted changes keep the box un-selected and click on "Revert this commit" button.

    Ranadheer Reddy

    Posted 2009-05-29T18:09:14.627

    Reputation: 2 703


    You can undo your Git commits in two ways: First is you can use git revert, if you want to keep your commit history:

    git revert HEAD~3
    git revert <hashcode of commit>

    Second is you can use git reset, which would delete all your commit history and bring your head to commit where you want it.

    git reset <hashcode of commit>
    git reset HEAD~3

    You can also use the --hard keyword if any of it starts behaving otherwise. But, I would only recommend it until it's extremely necessary.


    Posted 2009-05-29T18:09:14.627

    Reputation: 140


    Just use git reset --hard <last good SHA> to reset your changes and give new commit. You can also use git checkout -- <bad filename>.


    Posted 2009-05-29T18:09:14.627

    Reputation: 323


    Here is site: Oh shit, git!.

    Here are many recipes how to undo things in Git. Some of them:

    Oh shit, I need to change the message on my last commit!

    git commit --amend
    # follow prompts to change the commit message

    Oh shit, I accidentally committed something to master that should have been on a brand new branch!

    # Create a new branch from the current state of master
    git branch some-new-branch-name
    # Remove the commit from the master branch
    git reset HEAD~ --hard
    git checkout some-new-branch-name
    # Your commit lives in this branch now :)

    Eugen Konkov

    Posted 2009-05-29T18:09:14.627

    Reputation: 5 653


    I have found this site which describes how to undo things that you have committed into the repository.

    Some commands:

    git commit --amend        # Change last commit
    git reset HEAD~1 --soft   # Undo last commit

    Eugen Konkov

    Posted 2009-05-29T18:09:14.627

    Reputation: 5 653


    In IntelliJ IDEA you can just open the Git repository log by pressing Alt+9, right mouse button click at some tag from the commits list, and select: "Reset Current Branch to Here...".

    Krzysztof Walczewski

    Posted 2009-05-29T18:09:14.627

    Reputation: 399


    You can undo your commits from the local repository. Please follow the below scenario.

    In the below image I check out the 'test' branch (using Git command git checkout -b test) as a local and check status (using Git command git status) of local branch that there is nothing to commit.

    Enter image description here

    In the next image image you can see here I made a few changes in Filter1.txt and added that file to the staging area and then committed my changes with some message (using Git command git commit -m "Doing commit to test revert back").

    "-m is for commit message"

    Enter image description here

    In the next image you can see your commits log whatever you have made commits (using Git command git log).

    Enter image description here

    So in the above image you can see the commit id with each commit and with your commit message now whatever commit you want to revert back or undo copy that commit id and hit the below Git command, git revert {"paste your commit id"}. Example:

    git revert 9ca304ed12b991f8251496b4ea452857b34353e7

    Enter image description here

    I have reverted back my last commit. Now if you check your Git status, you can see the modified file which is Filter1.txt and yet to commit.

    Enter image description here

    Raj S. Rusia

    Posted 2009-05-29T18:09:14.627

    Reputation: 427


    Find the last commit hash code by seeing the log by:

    git log


    git reset <the previous co>

    Praveen Singh

    Posted 2009-05-29T18:09:14.627

    Reputation: 20



    Before reset commit we should know about HEAD... HEAD is nothing but your current state in your working directory. It is represented by a commit number.

    Git commit:

    Each change assigned under a commit which is represented by a unique tag. Commits can't be deleted. So if you want your last commit, you can simply dive into it using git reset.

    You can dive into the last commit using two methods:

    Method 1: (if you don't know the commit number, but want to move onto the very first)

    git reset HEAD~1  # It will move your head to last commit

    Method 2: (if you know the commit you simply reset onto your known commit)

    git reset 0xab3 # Commit number

    Note: if you want to know a recent commit try git log -p -1

    Here is the graphical representation:

    Enter image description here

    Mohideen ibn Mohammed

    Posted 2009-05-29T18:09:14.627

    Reputation: 6 869


    enter image description here

    Assuming you're working in Visual Studio, if you go in to you branch history and look at all of your commits, simply select the event prior to the commit you want to undo, right-click it, and select Revert. Easy as that.

    Uchiha Itachi

    Posted 2009-05-29T18:09:14.627

    Reputation: 844


    For sake of completeness, I will give the one glaringly obvious method that was overlooked by the previous answers.

    Since the commit was not pushed, the remote was unchanged, so:

    1. Delete the local repository.
    2. Clone the remote repository.

    This is sometimes necessary if your fancy Git client goes bye-bye.

    Don't forget to re-commit your saved changes since the last push.

    Dominic Cerisano

    Posted 2009-05-29T18:09:14.627

    Reputation: 1 401


    Rebasing and dropping commits are the best when you want to keep the history clean useful when proposing patches to a public branch etc.

    If you have to drop the topmost commit then the following one-liner helps

    git rebase --onto HEAD~1 HEAD

    But if you want to drop 1 of many commits you did say

    a -> b -> c -> d -> master

    and you want to drop commit 'c'

    git rebase --onto b c

    This will make 'b' as the new base of 'd' eliminating 'c'


    Posted 2009-05-29T18:09:14.627

    Reputation: 349


    If you would like to eliminate the wrong files you should do

    git reset --soft <your_last_good_commit_hash_here> Here, if you do git status, you will see the files in the staging area. You can select the wrong files and take them down from the staging area.

    Like the following.

    git reset wrongFile1 wrongFile2 wrongFile3

    You can now just add the files that you need to push,

    git add goodFile1 goodFile2

    commit them

    git commit -v or git commit -am "Message"

    and push

    git push origin master

    However, if you do not care about the changed files you can hard reset to previous good commit and push everything to the server.


    git reset --hard <your_last_good_commit_hash_here>

    git push origin master

    If you already published your wrong files to the server, you can use the --force flag to push to the server and edit the history.

    git push --force origin master

    sony vizio

    Posted 2009-05-29T18:09:14.627

    Reputation: 2 724


    Try this, hard reset to previous commit where those files were not added, then:

    git reset --hard <commit_hash>

    Make sure you have a backup of your changes just in case, as it's a hard reset, which means they'll be lost (unless you stashed earlier)


    Posted 2009-05-29T18:09:14.627

    Reputation: 1 043


    What i do each time i need to undo a commit/commits are:

    1. git reset HEAD~<n> // the number of last commits i need to undo
    2. git status // optional. All files are now in red (unstaged).

    3. Now, i can add & commit just the files that i need:

      • git add <file names> & git commit -m "message" -m "details"
    4. Optional: I can rollback the changes of the rest files, if i need, to their previous condition, with checkout:
      • git checkout <filename>
    5. if i had already pushed it to remote origin, previously:
      • git push origin <branch name> -f // use -f to force the push.

    Theo Itzaris

    Posted 2009-05-29T18:09:14.627

    Reputation: 2 361


    If you want to undo the very first commit in your repo

    You'll encounter this problem:

    $ git reset HEAD~
    fatal: ambiguous argument 'HEAD~': unknown revision or path not in the working tree.
    Use '--' to separate paths from revisions, like this:
    'git <command> [<revision>...] -- [<file>...]'

    The error occurs because if the last commit is the initial commit (or no parents) of the repository, there is no HEAD~.


    If you want to reset the only commit on "master" branch

    $ git update-ref -d HEAD
    $ git rm --cached -r .


    Posted 2009-05-29T18:09:14.627

    Reputation: 6 592


    Get last commit id by using this command: (in log one in the top is the latest one)

    git log

    Get the commit id (guid) and run this command:

    git revert <commit_id>

    Nalan Madheswaran

    Posted 2009-05-29T18:09:14.627

    Reputation: 4 732


    git push --delete (branch_name) //this will be removing the public version of your branch
    git push origin (branch_name) //This will add the previous version back


    Posted 2009-05-29T18:09:14.627

    Reputation: 1 167


    git revert commit

    This will generate the opposite changes from the commit which you want to revert back, and then just commit that changes. I think this is the simplest way.

    Gjorgi Gjorgiev

    Posted 2009-05-29T18:09:14.627

    Reputation: 51


    How to edit an earlier commit

    Generally I don't want to undo a bunch of commits, but rather edit an earlier commit to how I wish I had committed it in the first place.

    I found myself fixing a past commit frequently enough that I wrote a script for it.

    Here's the workflow:

    1. git commit-edit <commit-hash>

      This will drop you at the commit you want to edit.

      The changes of the commit will be unstaged, ready to be staged as you wish it was the first time.

    2. Fix and stage the commit as you wish it had been in the first place.

      (You may want to use git stash save --keep-index to squirrel away any files you're not committing)

    3. Redo the commit with --amend, eg:

      git commit --amend
    4. Complete the rebase:

      git rebase --continue

    Call this following git-commit-edit and put it in your $PATH:

    # Do an automatic git rebase --interactive, editing the specified commit
    # Revert the index and working tree to the point before the commit was staged
    set -euo pipefail
    warn () { printf '%s: %s\n' "$script_name" "$*" >&2; }
    die () { warn "[email protected]"; exit 1; }
    [[ $# -ge 2 ]] && die "Expected single commit to edit. Defaults to HEAD~"
    # Default to editing the parent of the most recent commit
    # The most recent commit can be edited with `git commit --amend`
    commit=$(git rev-parse --short "${1:-HEAD~}")
    # Be able to show what commit we're editing to the user
    if git config --get alias.print-commit-1 &>/dev/null; then
      message=$(git print-commit-1 "$commit")
      message=$(git log -1 --format='%h %s' "$commit")
    if [[ $OSTYPE =~ ^darwin ]]; then
      sed_inplace=(sed -Ei "")
      sed_inplace=(sed -Ei)
    export GIT_SEQUENCE_EDITOR="${sed_inplace[*]} "' "s/^pick ('"$commit"' .*)/edit \\1/"'
    git rebase --quiet --interactive --autostash --autosquash "$commit"~
    git reset --quiet @~ "$(git rev-parse --show-toplevel)"  # Reset the cache of the toplevel directory to the previous commit
    git commit --quiet --amend --no-edit --allow-empty  #  Commit an empty commit so that that cache diffs are un-reversed
    echo "Editing commit: $message" >&2

    Tom Hale

    Posted 2009-05-29T18:09:14.627

    Reputation: 6 037


    If you simply want to trash all your local changes/commits and make your local branch look like the origin branch you started from...

    git reset --hard origin/branch-name


    Posted 2009-05-29T18:09:14.627

    Reputation: 17 992