26803

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

How do I undo those commits from the local repository?

20
  • 799
    You 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
    Commented Mar 20, 2018 at 1:45
  • 63
    @YiminRong That can be done with Git's alias feature: git-scm.com/book/en/v2/Git-Basics-Git-Aliases
    – Edric
    Commented Oct 5, 2018 at 14:50
  • 163
    For VsCode users , just type ctrl +shift +G and then click on three dot ,ie , more options and then click on undo Last Commit
    – ashad
    Commented Apr 8, 2019 at 12:15
  • 38
    @YiminRong Undo what exactly? There are dozens of very different functional cases where "undoing" means something completely different. I'd bet adding a new fancy "magic wand" would only confuse things more. Commented Mar 24, 2020 at 14:27
  • 61
    @YiminRong Not buying it. People would still fumble and undo things not to be undone. But more importantly, git reflog is already close to what you describe, but gives the user more control on what's to be (un)done. But please, no, "undo" does not work the same everywhere, and people would expect many different things for the feature to achieve. Undo last commit? Undo last action? If last action was a push, undo how exactly, (reset and push) or (revert and push)? Commented Mar 25, 2020 at 13:23

101 Answers 101

29509
+500

Undo a commit & redo

$ git commit -m "Something terribly misguided" # (0: Your Accident)
$ git reset HEAD~                              # (1)
# === If you just want to undo the commit, stop here! ===
[ edit files as necessary ]                    # (2)
$ git add .                                    # (3)
$ git commit -c ORIG_HEAD                      # (4)
  1. git reset is the command responsible for the undo. It will undo your last commit while leaving your working tree (the state of your files on disk) untouched. You'll need to add them again before you can commit them again.
  2. Make corrections to working tree files.
  3. git add anything that you want to include in your new commit.
  4. 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.

Alternatively, to edit the previous commit (or just its commit message), commit --amend will add changes within the current index to the previous commit.

To remove (not revert) a commit that has been pushed to the server, rewriting history with git push origin main --force[-with-lease] is necessary. It's almost always a bad idea to use --force; prefer --force-with-lease instead, and as noted in the git manual:

You should understand the implications of rewriting history if you amend a commit that has already been published.


Further Reading

You can use git reflog to determine the SHA-1 for the commit to which you wish to revert. Once you have this value, use the sequence of commands as explained above.


HEAD~ is the same as HEAD~1. The article What is the HEAD in git? is helpful if you want to uncommit multiple commits.

27
  • 611
    And if the commit was to the wrong branch, you may git checkout theRightBranch with all the changes stages. As I just had to do. Commented Oct 5, 2010 at 15:44
  • 598
    If 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
    Commented Apr 13, 2011 at 14:15
  • 168
    zsh users might get: zsh: no matches found: HEAD^ - you need to escape ^ i.e. git reset --soft HEAD\^
    – tnajdek
    Commented Feb 21, 2013 at 17:47
  • 31
    The 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
    Commented Jul 2, 2014 at 21:19
  • 79
    I've googled & hit this page about 50 times, and I always chuckle at the first line of code git commit -m "Something terribly misguided"
    – 100pic
    Commented Aug 21, 2019 at 4:25
12886

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. I'll show you the 4 different ways you can undo a commit.

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

   (F)
A-B-C
    ↑
  master

Option 1: git reset --hard

You want to destroy commit C and also throw away any uncommitted changes. You do this:

git reset --hard HEAD~1

The result is:

 (F)
A-B
  ↑
master

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

Option 2: git reset

Maybe 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:

   (F)
A-B-C
    ↑
  master

Do this, leaving off the --hard:

git reset HEAD~1

In this case the result is:

   (F)
A-B-C
  ↑
master

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!

Option 3: git reset --soft

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.

Option 4: you did git reset --hard and need to get that code back

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 this

git reflog

and you'll see a list of (partial) commit SHAs (that is, hashes) 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.

29
  • 83
    BEWARE! 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. Commented Feb 20, 2013 at 18:46
  • 32
    Consider 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. Commented Aug 13, 2013 at 14:37
  • 190
    Missing 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 <bad-commit-sha1-id> git push origin : Commented Nov 8, 2013 at 23:43
  • 43
    @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
    Commented Nov 9, 2013 at 0:00
  • 45
    @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. Commented Nov 9, 2013 at 3:02
2769

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 I 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 information, check out Git Basics - Undoing Things.

4
  • 140
    I 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
    Commented Jul 14, 2011 at 8:32
  • 10
    If Git asks you "More?" when you try these commands, use the alternate syntax on this answer: stackoverflow.com/a/14204318/823470
    – tar
    Commented Mar 5, 2020 at 15:50
  • 1
    revert deleted some files I add added to my repo. Use it with caution!
    – carloswm85
    Commented Jul 31, 2021 at 22:25
  • Specifically, if Git asks you "More?" when you try these commands on WIndows, you need to do 'git reset --hard "HEAD^"'. The doublequotes around "HEAD^" prevent Windows from interpreting ^ as a metacharacter.
    – Some Guy
    Commented Jun 6 at 21:00
1955

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.

4
  • 28
    FYI: This removes all my files and I lost changes. Commented May 15, 2020 at 10:03
  • UPD: However, I've restored it using reflog. But the receipt did not work for the initial commit. Commented May 15, 2020 at 11:25
  • 14
    Use git rm --cached to keep the files in the filesystem and only delete them from the git index!
    – xuiqzy
    Commented May 19, 2020 at 10:00
  • " git --amend --no-edit " will commit your last changes to the commit before them.
    – alex
    Commented Mar 26, 2022 at 22:22
1302

This will add a new commit which deletes the added files.

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

Or you can rewrite history to undo the last commit.

Warning: this command will permanently remove the modifications to the .java files (and any other files) that you committed -- and delete all your changes from your working directory:

git reset --hard HEAD~1

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

5
  • 31
    git commit -a -m "" or git commit -am "" naturally! :]
    – trejder
    Commented Jun 21, 2014 at 16:31
  • Another 'shortcut' use of stash; if you want to unstage everything (undo git add), just git stash, then git stash pop Commented Dec 8, 2015 at 22:30
  • 24
    Gosh... this answer should be deleted. git reset --hard HEAD-1 is incredibly dangerous - it's not just "undoing the commit", it also delete all of your changes, which is not what OP asked for. I unfortunately applied this answer (which StackOverflow for no reason shows first than the accepted one with 26k upvotes), and now will struggle to recover all of my changes.
    – Jack
    Commented Jun 7, 2022 at 13:19
  • 9
    For those who did not read and just ran the above command like me, I hope you learned your lesson now XD... To undo command, git reflog to find the discarded commit and run git reset --hard $1 where $1 is your discarded commit
    – seantsang
    Commented Aug 11, 2022 at 4:10
  • Please ignore this answer and look at the other, better upvoted response. It will save you a lot of trouble. Commented Aug 5 at 16:52
964

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.

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.

Reflog

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

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

2
  • 7
    For those reading in future - please note that git revert is a separate command - which basically 'resets' a single commimt.
    – BenKoshy
    Commented Aug 8, 2018 at 7:11
  • Adding to the reply of @BenKoshy - please also note that git revert will create a new commit that inverses the given changes. It is a safer alternative to git reset.
    – gsan
    Commented Nov 17, 2022 at 22:24
851

Use git revert <commit-id>.

To get the commit ID, just use git log.

5
  • 23
    What 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? Commented Jan 13, 2015 at 22:05
  • I just utilized git revert commit-id worked like a charm. Of course then you will need to push your changes. Commented Jan 25, 2016 at 21:07
  • 12
    I believe that would be git cherry-pick <<erroneous-commit-sha>> @astronomerdave. From, Mr. Almost-2-Years-Late-to-the-Party.
    – Tom Howard
    Commented Oct 20, 2016 at 18:19
  • @Kris: Instead of cherry-pick use rebase. Because it is advanced cherry-picking Commented Nov 10, 2018 at 9:38
  • 5
    I'd use revert only if I've already pushed my commit. Otherwise, reset is a better option. Don't forget that revert creates a new commit, and usually this is not the goal. Commented Feb 6, 2020 at 12:40
702

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.

More (link broken) (Archived version)

2
  • 20
    @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”. Commented Dec 14, 2015 at 15:34
  • git reset --hard HEAD^1 gives me this error "fatal: ambiguous argument 'HEAD1': unknown revision or path not in the working tree."
    – Rob Mosher
    Commented Dec 20, 2020 at 10:55
629

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

1
  • Yes! Git extras are really nice. I just can't use Git without them, so convenient (brew install git-extras).
    – COil
    Commented Nov 2 at 19:01
590

I wanted to undo the latest five 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)
prompt>
3
  • 34
    Rewriting 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
    Commented Dec 7, 2012 at 16:02
  • 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
    Commented Dec 8, 2012 at 14:14
  • 9
    "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: stackoverflow.com/a/2046748/334451 Commented Aug 7, 2013 at 10:10
549

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.

0
512

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

Clean up 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.

3
  • 13
    gitk --all $(git reflog | cut -c1-7)& may be helpful for finding the previous revision if you want to undo an '--amend' commit. Commented Oct 18, 2014 at 23:38
  • 8
    It 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
    Commented Sep 4, 2015 at 4:52
  • Correcting a private branch in remote repository can also be done by simply git push origin (branch_name) --force Commented Sep 7, 2018 at 12:09
426

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 like:

git reset --hard <commit_id>

git push origin <branch_name> -f
3
  • 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". Commented Sep 27, 2017 at 23:30
  • 7
    @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
    Commented Sep 28, 2017 at 13:10
  • Worked like a charm. The branch being reset ('master') was not checked-out by any one, so no chance of any conflict.
    – Gsv
    Commented Apr 24, 2023 at 12:07
416

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.

OR

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.

1
  • 8
    Probably 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
    Commented Dec 7, 2014 at 0:38
358

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

Or:

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.
0
329

A single command:

git reset --soft 'HEAD^' 

It works great to undo the last local commit!

1
  • 18
    I needed to write git reset --soft "HEAD^" with double quotes, because I write it from Windows command prompt.
    – Ena
    Commented Apr 23, 2014 at 9:13
311

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?

0
300

"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.

0
201

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
187

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.

0
184

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.

173

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

git reset <the previous co>
168

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

166

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.

2
  • 10
    The very useful description about differences --soft VS --hard atlassian.com/git/tutorials/… Commented Dec 15, 2016 at 16:29
  • 4
    One doesn't really lose the commits on a --hard reset as they will be available in the ref log for 30 days git reflog.
    – Todd
    Commented Sep 11, 2017 at 14:10
162

Simple, run this in your command line:

git reset --soft HEAD~ 
0
155

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>

or

$ git reset --hard HEAD~<n>

COMMIT-ID: ID for the commit

n: is the 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 multiply 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

or

$ git reset --hard HEAD~1

Suppose you want to revert the commit 'be20eb8':

$ git reset --hard be20eb8

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

$ git reset --help
3
  • 11
    git reset --hard HEAD~1 is too dangerous! This will not just 'cancel last commit', but will revert repo completely back to the previous commit. So you will LOOSE all changes committed in the last commit! Commented Mar 21, 2017 at 12:09
  • You right, to undo this you can use git push -f <remote> HEAD@{1}:<branch>
    – Benny
    Commented Apr 24, 2017 at 13:07
  • 1
    Unfortunately, I use --hard, and my files are deleted! I did not check the comment first because it is collapsed. Do not use --hard if you do not know what you are doing!
    – anonymous
    Commented Aug 19, 2018 at 13:53
151

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.

149

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).

137

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

107

A simple step-by-step guide is as follows:

  • Destroy a commit and throw away any uncommitted changes

    git reset --hard HEAD~1
    
  • Undo the commit, but keep your changes

    git reset HEAD~1
    
  • Keep your files, and stage all changes back automatically

    git reset --soft HEAD~1
    
  • Resurrect a commit you destroyed

    git reflog # To find the sh
    
2
  • 2
    What do you mean by "To find the sh"? Do you mean "To find the SHA-1 hash value"? Or something else? Commented Mar 30, 2023 at 11:40
  • Yes, sh is a thing too...
    – mirekphd
    Commented Aug 10, 2023 at 16:34

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