430

How can I undo every change made to my directory after the last commit, including deleting added files, resetting modified files, and adding back deleted files?

Shirish Herwade
  • 11,461
  • 20
  • 72
  • 111
Dogbert
  • 212,659
  • 41
  • 396
  • 397
  • possible duplicate of [Revert to previous Git commit](http://stackoverflow.com/questions/4114095/revert-to-previous-git-commit) – nawfal Feb 09 '14 at 18:45
  • 8
    @nawfal may be a duplicate, but 'reset all changes after last commit' matches more searching criteria(words searched in google) than corresponding 'how to revert git repository'. At least for people like me who don't have English as their mother tongue :d – Shirish Herwade Aug 04 '17 at 07:33

3 Answers3

750

First, reset any changes

This will undo any changes you've made to tracked files and restore deleted files:

git reset HEAD --hard

Second, remove new files

This will delete any new files that were added since the last commit:

git clean -fd

Files that are not tracked due to .gitignore are preserved; they will not be removed

Warning: using -x instead of -fd would delete ignored files. You probably don't want to do this.

Jim U
  • 3,318
  • 1
  • 14
  • 24
Benjamin Bannier
  • 55,163
  • 11
  • 60
  • 80
106

How can I undo every change made to my directory after the last commit, including deleting added files, resetting modified files, and adding back deleted files?

  1. You can undo changes to tracked files with:

    git reset HEAD --hard
    
  2. You can remove untracked files with:

    git clean -f
    
  3. You can remove untracked files and directories with:

    git clean -fd
    

    but you can't undo change to untracked files.

  4. You can remove ignored and untracked files and directories

    git clean -fdx
    

    but you can't undo change to ignored files.

You can also set clean.requireForce to false:

git config --global --add clean.requireForce false

to avoid using -f (--force) when you use git clean.

Ortomala Lokni
  • 56,620
  • 24
  • 188
  • 240
-9

There are two commands which will work in this situation,

root>git reset --hard HEAD~1

root>git push -f

For more git commands refer this page

RKS
  • 445
  • 4
  • 14