How to reset local git repository?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Resetting a local Git repository can mean very different actions, from undoing unstaged edits to fully matching a remote branch. Choosing the wrong reset command can permanently discard work. The safest workflow is to identify your target state first, then run the minimum command required.
Decide the Reset Goal First
Most requests to reset a repository fall into one of these cases:
- Discard unstaged edits in tracked files.
- Unstage files but keep file content.
- Move branch pointer to an earlier commit.
- Force local branch to match remote state.
- Remove untracked files and directories.
Map your need to the correct command before running anything destructive.
Check Current State Before Reset
Always inspect the repository first.
If there is any chance you need current work later, create a safety branch or stash.
This takes seconds and can prevent major data loss.
Reset Tracked File Changes Only
To discard local edits in tracked files and return to HEAD:
This is usually safer and clearer than older checkout syntax. It does not remove untracked files.
To unstage files while keeping their content:
Use this when git add was accidental but edits should remain.
Move Branch Pointer with git reset
git reset changes branch history position. Pick mode carefully:
--soft: moveHEAD, keep changes staged.--mixeddefault: moveHEAD, keep changes unstaged.--hard: moveHEADand discard tracked file changes.
Example, undo last commit but keep file content unstaged:
Example, fully discard last commit and local tracked edits:
Use hard reset only when you are sure discarded changes are not needed.
Fully Match Remote Branch
If local branch should be identical to origin/main:
This removes local tracked differences against remote tip. If you also need to remove untracked files:
Preview clean results first:
Dry run output helps avoid deleting generated files you still need.
Recover If You Reset Too Far
Even after a bad reset, recovery is often possible through reflog.
Find the commit before the reset and restore branch pointer:
Reflog entries expire over time, so recover quickly after mistakes.
Recommended Team Workflow
In shared repositories:
- Prefer non-destructive options first.
- Avoid rewriting published branch history unless team policy allows it.
- Announce force push events if reset is followed by
git push --force-with-lease.
For personal feature branches, resets are fine, but keep a temporary backup branch before major cleanup.
Common Pitfalls
- Running
git reset --hardwithout checking current branch. - Using
git clean -fdwithout a dry run and deleting needed files. - Forgetting to stash or branch off before destructive operations.
- Resetting local branch to remote and losing unpushed commits.
- Force pushing rewritten history to shared branches without coordination.
- Treating every local reset request as the same problem when the desired end state is different.
Summary
- Define exactly what reset outcome you need before choosing commands.
- Use
git statusand safety snapshots before destructive operations. - Prefer
git restorefor file-level cleanup andgit resetfor history moves. - Use dry-run
git clean -fdnbefore deleting untracked files. - Use
git reflogquickly if you need to recover from a mistaken reset.
Related reading
- How to resolve Error bad index – Fatal index file corrupt when using Git
- How to resolve Error bad index – Fatal index file corrupt when using Git
- How to resolve git did not exit cleanly exit code 128 error on TortoiseGit?
- How to resolve git stash conflict without commit?
- How to resolve git's not something we can merge error
- How to resume a git pull/clone after a hung up unexpectedly?
- How to resume review process after updating pull request at GitHub?
- How to retrieve a single file from a specific revision in Git?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.