Menu
Coddy logo textTech

Git Reset --hard

Last updated

git reset --hard moves your branch to a target commit and forces both the staging area and your working tree to match it exactly - discarding every uncommitted change along the way. It's the most destructive form of git reset, so be sure you don't need your current work before running it.

Reset away a commit by mistake? A hard reset is usually recoverable via git reflog. Try these safely in the terminal playground - a real shell in your browser.

git reset --hard moves HEAD back and discards everything after it - staging area and working tree included.

Syntax

CommandWhat it does
git reset --hardDiscard all uncommitted changes (stay on HEAD)
git reset --hard HEAD~1Drop the last commit and its changes
git reset --hard <hash>Force the branch back to a specific commit
git reset --hard origin/mainMake the local branch match the remote exactly

Recovering a hard reset

Committed work you reset away is usually still recoverable.

StepCommandResult
1git reflogList where HEAD has been, with hashes
2git reset --hard <hash>Jump back to the commit before the reset

Git reset --hard FAQ

What does git reset --hard actually do?
It moves your current branch to the target commit and overwrites both the staging area and your working tree to match it. Any uncommitted changes - staged or not - are discarded. Compared with --soft (which keeps changes staged) and --mixed (which keeps them unstaged), --hard is the only mode that throws the changes away.
How do I discard all my local changes?
Run git reset --hard with no commit to reset the working tree to the last commit, wiping uncommitted edits. To also clear untracked files that reset doesn't touch, follow it with git clean -fd.
How do I reset my branch to match the remote?
Run git fetch then git reset --hard origin/main (swap in your branch). This forces your local branch to exactly match the remote, discarding local commits and changes that diverge from it - useful when your local branch has gone wrong and you just want the remote's version.
Can I undo a git reset --hard?
Committed work usually yes: run git reflog to find the commit your branch pointed at before the reset, then git reset --hard <hash> to return to it. Changes that were never committed (only in your working tree) can't be recovered, so treat --hard with care.
Can I practice this online?
Yes. Open the terminal playground to run git reset --hard in a real shell in your browser - nothing to install. Coddy's free interactive Git course also covers resetting and undoing changes step by step.
Coddy programming languages illustration

Learn Git with Coddy

GET STARTED