Git Reset Hard
What
The git reset --hard command rolls back your commit history to a specific target commit and completely discards all modifications, unstaged changes, and staged changes in your workspace, forcing your working directory to match the target commit exactly.
How
Provide the target commit hash you want to force-align your repository to:
# Completely roll back and discard all local modifications since the target commit
git reset --hard <target_commit_hash>
Running this command:
- Moves the branch pointer back to the specified commit.
- Wipes out any modified code in your staging area (index).
- Deletes any unstaged modifications in your working directory.
Why
Sometimes, an experiment or a feature path goes completely wrong, and you want to throw away your recent work and start fresh from a known, stable checkpoint. git reset --hard is the quickest way to purge all local edits and align your entire workspace with a working state in your repository’s history.
Summary
Use git reset --hard <commit_hash> to permanently discard all local edits and force your working directory and staging area to match a specific historical commit exactly.