Back to roadmaps git Course

Reverting Local Actions: git restore and git reset

If you make a mistake in your local files, you can discard those changes or reset them from the staging area.


1. Discarding Working Directory Changes

If you modified a file but want to revert it to the state of the last commit:

# Discard uncommitted changes in a specific file
git restore index.html

# Discard all uncommitted changes in the current directory
git restore .

2. Unstaging Files from the Index

If you ran git add to stage a file but decide you do not want to commit it yet:

# Remove a file from the staging area, keeping its edits intact
git restore --staged styles.css

3. Undoing Commits Locally

To undo a local commit, use git reset:

  • Soft Reset: Undoes the commit but keeps your changes staged in the index:
git reset --soft HEAD~1
  • Mixed Reset (Default): Undoes the commit and unstages your changes, keeping them in your working directory:
git reset HEAD~1
  • Hard Reset: Caution! Undoes the commit, unstages your changes, and deletes all modifications in your working directory:
git reset --hard HEAD~1
Published on Last updated: