How do you reset your working directory to the last commit in Git?

How do you reset your working directory to the last commit in Git?

a) git reset --hard
b) git revert HEAD
c) git checkout --last
d) git reset HEAD

Answer:

a) git reset --hard

Explanation:

The git reset --hard command resets your working directory, index (staging area), and HEAD pointer to the state of the last commit. It discards all changes in the working directory and the staging area, effectively reverting the repository to the last committed state.

This command is useful when you want to discard all uncommitted changes and return to a clean working directory.

Be cautious when using git reset --hard as it permanently deletes uncommitted changes.

Reference:

Top 100 Git and GitHub MCQ Questions and Answers

Scroll to Top