Search code examples
gitresetrevert

"git reset --hard" command and "git clean -f" not setting staging area like most recent commit


I cloned a repository, toyed around a bit and (oops) made a mess of my code. Now I just want to revert to a clean clone of the repository code and start over.

I attempted to use the following from the terminal of the working directory:

git reset --hard      # deletes watched filed in local working directory and staged area
git clean -n          # shows what will be deleted without actually deleting anything (for the faint of heart)
git clean -f          # deletes unwatched files

However, when I run git status, it still shows the messy files.

Is there a recommended way to reset your machine to the latest check in?


Solution

  • git clean -n only does a "dry-run" of git clean to show you what it will delete, it doesn't actually delete anything. You can run the following sequence of commands to clean up your working copy and staging area/index:

    git reset -- .    # Clear the index/staging area
    git checkout -- . # Revert changes in your working copy
    git clean -df     # Recursively delete untracked files.
    

    If you want, you could even make these into a git alias:

    # Make the alias
    git config --global alias.undo \
    "!git reset -- . ; git checkout -- . ; git clean -df; git status"
    
    # Usage
    git undo