Discarding changes in working directory in Git

To discard changes in working tree to make it clean, a safer way is to use git stash command. git stash takes the changes in working directory and index away and you can reapply them back later.

# Stash changes in working directory
$ git stash

# Apply the latest stashed changes
$ git stash apply

Read stashing changes for more about how to use git stash.

If you just want to drop the changes directly, you can run git reset command (Be careful to use it for changes can not be recovered):

# Discard changes in working directory and index.
# Be aware that changes can not be recovered.
# --hard, discard changes in index and working tree
$ git reset --hard

If you want to make your working directory totally clean, which means removing those untracked files. You can continue to run git clean to remove untracked files or even including ignored files. See deleting untracked files for how to do that.

Leave a Reply