Skip to content

Instantly share code, notes, and snippets.

@silvioramalho
Created April 17, 2025 19:18
Show Gist options
  • Save silvioramalho/21871d1a9819472e5abde94bccba9f11 to your computer and use it in GitHub Desktop.
Save silvioramalho/21871d1a9819472e5abde94bccba9f11 to your computer and use it in GitHub Desktop.
Understanding git reset: soft, mixed, and hard

Understanding git reset: soft, mixed, and hard

This document explains the three main types of git reset and when to use each.


1. git reset --soft HEAD~2

What it does:

  • Removes the last two commits.
  • Keeps all changes in the staging area (index).
  • Nothing is lost; changes are ready to be committed again.

Use when:

  • You want to rewrite or squash commits.
  • You want to change the commit message or combine multiple commits.

Example:

git reset --soft HEAD~2
git commit -m "new single commit message"
git push --force

2. git reset --mixed HEAD~2 (default)

What it does:

  • Removes the last two commits.
  • Keeps changes in the working directory, but unstages them.
  • You'll need to git add again before committing.

Use when:

  • You want to reselect or re-stage the changes.
  • You want to edit or clean up before committing again.

Example:

git reset --mixed HEAD~2
# make changes or stage selected files
git add .
git commit -m "cleaned up commit"
git push --force

3. git reset --hard HEAD~2

What it does:

  • Removes the last two commits.
  • Deletes all changes in those commits and in the working directory.
  • Irreversible unless backed up.

Use when:

  • You are sure you don’t want to keep the changes.
  • You want a clean state of the third-to-last commit.

Example:

git reset --hard HEAD~2
git push --force

Comparison Table

Command Commits removed Staging area Working directory
--soft
--mixed (default)
--hard

Reminder

Use --force only when you're sure that no one else is working on the branch, as it will overwrite the remote history.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment