Git Reset

2 min

Table of contents

  1. What Is Reset?
  2. The 3 Reset Modes
  3. Reset Soft
  4. Reset Mixed
  5. Reset Hard
  6. Danger and Precautions
  7. Practical Commands

1. What Is Reset?

Git reset = Go back in time by undoing commits.

Concept:

  • Move the branch to an earlier commit
  • 3 modes depending on what you keep or delete
  • DESTRUCTIVE if used badly

Analogy: It is like erasing pages from a notebook.

Back to table of contents

2. The 3 Reset Modes

ModeCommitsIndex/StagingWorking Directory
--softUndoesKeepsKeeps
--mixedUndoesClearsKeeps
--hardUndoesClearsClears

Simple memo:

  • Soft = just the commits
  • Mixed = commits + staging
  • Hard = EVERYTHING (dangerous!)

Back to table of contents

3. Reset Soft

Usage: Redo the last commit with more changes.

bash
mkdir demo-reset && cd demo-reset
git init

echo "print('v1')" > app.py && git add . && git commit -m "Version 1"
echo "print('v2')" > app.py && git add . && git commit -m "Version 2"

# Soft reset: undo commit, keep everything in staging
git reset --soft HEAD~1

git status                    # Staged changes ready
git log --oneline            # One commit less
cat app.py                   # v2 content still there

# Recommit with an improved message
git commit -m "Version 2 - With new features"

Back to table of contents

4. Reset Mixed

Usage: Undo commits AND unstage (default mode).

bash
echo "print('v3')" > app.py && git add . && git commit -m "Version 3"

# Mixed reset (or just git reset)
git reset HEAD~1

git status                   # UNSTAGED changes
git log --oneline           # Commits undone
cat app.py                  # v3 content still there, but not staged

# Must re-add before commit
git add . && git commit -m "Version 3 - Redone cleanly"

Back to table of contents

5. Reset Hard

DANGER: Deletes EVERYTHING, even your uncommitted modifications!

bash
echo "print('v4')" > app.py && git add . && git commit -m "Version 4"

# Changes in progress
echo "print('work in progress')" >> app.py

# Hard reset: EVERYTHING DISAPPEARS
git reset --hard HEAD~1

git status                  # Working directory clean
cat app.py                 # Back to v2, work in progress LOST!

Usage: When you REALLY want to throw everything away and go back.

Back to table of contents

6. Danger and Precautions

Reset is DESTRUCTIVE with shared commits!

NEVER do this:

bash
git push origin main           # Commits shared with the team
git reset --hard HEAD~3        # Deletes 3 shared commits
git push --force              # Forces the deletion for everyone
# = CHAOS in the team!

Safety rule:

  • Reset OK on local (unpushed) commits
  • Reset FORBIDDEN on already shared commits
  • Alternative: git revert (safer)

Back to table of contents

7. Practical Commands

CommandAction
git reset HEAD~1Undo 1 commit (mixed)
git reset --soft HEAD~2Undo 2 commits, keep staging
git reset --hard HEAD~1Undo 1 commit, delete EVERYTHING
git reset <hash>Go back to a specific commit
git reset --hard origin/mainReset to the remote version

Reset = Powerful but dangerous. Use with caution!

Back to table of contents