How to use Git Revert

Revert a File

git checkout -- file_name

This will update the file file_name in working directory. It'll be from your local repository. Effectively, revert whatever you've done to the file.

Revert a Directory

git checkout HEAD -- dir_full_path

This will update the dir dir_full_path to the commit version in HEAD. (typically the last commit.) Replace HEAD by a commit hash to revert to other version.

Revert a File to Last Commit

# revert file to last commit
git checkout HEAD -- file_name
# revert file to the commit before last commit
git checkout HEAD^ -- file_name


Remove All Untracked Files

git clean -d -x -f
  • -d → Delete untracked dirs

  • -x → ignore the .gitignore file. Delete them.

  • -f → force it