git reset is the sharpest tool for rewriting commit history. It changes what your branch points to, and it can alter your working directory depending on the mode you choose. Understand it fully, or risk losing work.
What Git Reset Does
git reset moves the current branch HEAD to a specified commit. This affects three primary states in Git:
- HEAD – where your branch is pointing.
- Index (staging area) – the snapshot that will become your next commit.
- Working directory – the actual files on disk.
Modes of Git Reset
--soft
Moves HEAD to a target commit. Leaves index and working directory untouched. Staged changes remain staged. This is ideal for merging commits without altering code in progress.
--mixed (default)
Moves HEAD and resets the index to match the target commit. Keeps changes in the working directory. Best for undoing commits while keeping edits for revision.
--hard
Moves HEAD, resets the index, and overwrites the working directory to match the target commit. This discards changes. Use it only when you are sure you can lose all uncommitted work.
When to Use Git Reset
- Remove local commits before pushing to remote.
- Reorder or rewrite local commit history for clarity.
- Quickly rollback a branch to a stable state.
Git Reset vs. Git Revert
Git reset rewrites history and affects local branches directly. Git revert creates a new commit that undoes changes, preserving history. Use reset for local cleanup; revert for shared history safety.
Safety and Best Practices
Always check git log before using reset. For destructive resets, consider git reflog as a recovery lifeline. Avoid --hard in shared branches. Use --soft and --mixed for safer workflows.
Command Examples
# Soft reset to previous commit
git reset --soft HEAD~1
# Mixed reset to specific commit
git reset abc123
# Hard reset to origin/master
git reset --hard origin/master
Control your commits. Keep history lean. With mastery of git reset, your repository stays in shape and moves fast.
See it live in minutes at hoop.dev and push safe, precise resets without breaking flow.