Git Reset
Git Reset: Rewinding Local Branch Pointers and Staging
git reset moves your current branch pointer backward to an earlier commit, letting you unstage files or rewrite unpushed local commits. Depending on the mode flag you pass, Git either preserves your edits or wipes them from your working directory.
Comparing --soft, --mixed, and --hard
flowchart LR
Soft["--soft HEAD~1<br/>Keeps Staged + Working"] --> Mixed["--mixed HEAD~1 (default)<br/>Unstages, Keeps Working"]
Mixed --> Hard["--hard HEAD~1<br/>Discards Staged + Working"]
| Mode | Moves Branch HEAD |
Resets Staging Area | Resets Working Files | Safe for Unpushed Edits? |
|---|---|---|---|---|
--soft |
Yes | No (stays staged) | No | Yes |
--mixed (default) |
Yes | Yes (becomes unstaged) | No | Yes |
--hard |
Yes | Yes | Yes (deletes edits) | Destructive |
Practical Reset Examples
Choosing the right flag depends on whether you want to keep your edits staged, keep them unstaged in your editor, or erase them completely.
Use --soft when you created two messy local commits on feature/header-nav and want to squash them into one clean commit before pushing. Your files stay staged and ready to commit immediately:
git reset --soft HEAD~2
git commit -m "Implement header navigation and styles.css"
Use the default --mixed mode when you want to uncommit your latest snapshot, clear the staging area, and inspect or split your modified files again before recommitting:
git reset HEAD~1
Use --hard only when you want to throw away the last local commit and every uncommitted edit in index.html or styles.css:
git reset --hard HEAD~1
Common Reset Mistakes
Running git reset --hard on untracked edits or uncommitted changes destroys work that was never stored in Git's object database. Before running --hard, check git status or create a quick backup branch with git branch backup-wip.
Never run git reset to remove commits you already pushed to a shared branch; use git revert instead so teammates' histories stay compatible.
Frequently Asked Questions
What does HEAD~1 mean in git reset?
HEAD~1 refers to the parent commit immediately before your current HEAD commit. Similarly, HEAD~3 points three commits back along the first-parent chain.
How do I unstage a single file without resetting commits?
Use git restore --staged styles.css (or git reset HEAD -- styles.css) to remove just that file from the staging area while keeping your local edits intact.
Can I recover commits deleted by git reset --hard?
Yes, as long as those commits existed locally earlier. Run git reflog, find the previous commit hash, and restore it with git reset --hard <hash>.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With