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

StackOverflow https://stackoverflow.com/questions/22516468

  •  17-06-2023
  •  | 
  •  

Question

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?

Was it helpful?

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

OTHER TIPS

Nevermind, I used sudo git reset --hard and after entering my password, it cleared out my local repository history to a known state! Although, I didn't know about git aliases. That's great and I will be sure to use it!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top