git
repository
reset
local
guide

How to reset local git repository?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

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.

bash
git status
git branch --show-current
git log --oneline --decorate -n 5

If there is any chance you need current work later, create a safety branch or stash.

bash
git switch -c backup/pre-reset-2026-03-04
# or
git stash push -u -m "pre-reset safety snapshot"

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:

bash
git restore .

This is usually safer and clearer than older checkout syntax. It does not remove untracked files.

To unstage files while keeping their content:

bash
git restore --staged .

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: move HEAD, keep changes staged.
  • --mixed default: move HEAD, keep changes unstaged.
  • --hard: move HEAD and discard tracked file changes.

Example, undo last commit but keep file content unstaged:

bash
git reset --mixed HEAD~1

Example, fully discard last commit and local tracked edits:

bash
git reset --hard HEAD~1

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:

bash
git fetch origin
git reset --hard origin/main

This removes local tracked differences against remote tip. If you also need to remove untracked files:

bash
git clean -fd

Preview clean results first:

bash
git clean -fdn

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.

bash
git reflog -n 20

Find the commit before the reset and restore branch pointer:

bash
git reset --hard <commit_from_reflog>

Reflog entries expire over time, so recover quickly after mistakes.

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 --hard without checking current branch.
  • Using git clean -fd without 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 status and safety snapshots before destructive operations.
  • Prefer git restore for file-level cleanup and git reset for history moves.
  • Use dry-run git clean -fdn before deleting untracked files.
  • Use git reflog quickly if you need to recover from a mistaken reset.

Course illustration
Course illustration

All Rights Reserved.