GIT : RESET --hard

GIT : RESET --hard

If you messed up your code and you want to revert the changes you made, you can use

git reset --hard [commit]

where [commit] is the "commit hash" you want to go back to.

  • The files of the working directory and the staging area will be reset to the commit specified
  • Untracked files of your working directory will not be deleted


Example

git reset --hard f414f31

You can refer to HEAD as well

$ git reset --hard HEAD
/* going back to HEAD, that is the last commit of current branch */

$ git reset --hard HEAD^  
/* going back to the commit before HEAD */

$ git reset --hard HEAD~2  
/* going back two commits before HEAD */



Some other command that can be useful when you think about resetting

To see what files are in the staging area

git diff --name-only --staged

To see the untracked files

git ls-files --others --exclude-standard

To have a clear idea of where to go back to (it shows your current Git branch and its commits)

$ git log --oneline --graph


Did I write anything wrong ? Please let me know in the comments :-)