WE CODE NOW
  • Home 
  • Blog 
  • Guides 
Guides
  1. Home
  2. Guides
  3. Git
  4. Git Reset and Revert Commands Explained

Git Reset and Revert Commands Explained

Posted on June 7, 2024 • 2 min read • 242 words
Git
 
Reset
 
Revert
 
Version Control
 
Git
 
Reset
 
Revert
 
Version Control
 
Share via

Understand the differences between git reset and git revert commands and how to use them to modify commit history and undo changes.

On this page
  • Soft Reset
  • Mixed Reset (Default)
  • Hard Reset
  • Revert a Commit
  • Unstage a File
  • Undo a Commit but Keep Changes
  • Undo a Commit and Discard Changes
  • Amend the Last Commit
  • Restore a Deleted File
  • Restore to a Specific Commit

Git Reset and Revert Commands Explained  

Resetting and reverting are powerful Git commands for modifying commit history and undoing changes. Here’s how to use them:

Soft Reset  

git reset --soft HEAD~1

Moves the HEAD pointer to the previous commit, keeping changes in the staging area.

Mixed Reset (Default)  

git reset HEAD~1

Moves the HEAD pointer to the previous commit, keeping changes in the working directory but not staged.

Hard Reset  

git reset --hard HEAD~1

Moves the HEAD pointer to the previous commit, discarding all changes in the working directory.

Revert a Commit  

git revert commit-hash

Creates a new commit that undoes the changes from a specific commit.

Unstage a File  

git reset HEAD filename

Removes a file from the staging area.

Undo a Commit but Keep Changes  

git reset --soft HEAD~1

Undoes the last commit but keeps changes in the staging area.

Undo a Commit and Discard Changes  

git reset --hard HEAD~1

Undoes the last commit and discards all changes.

Amend the Last Commit  

git commit --amend -m "New commit message"

Modifies the most recent commit.

Restore a Deleted File  

git checkout -- filename

Restores a deleted file from the last commit.

Restore to a Specific Commit  

git reset --hard commit-hash

Resets the repository to a specific commit, discarding all changes.

Understanding how to use git reset and git revert effectively can help you manage your commit history and undo changes safely. Practice these commands to become proficient in their use.

 Customizing Git with Alias Commands
Git Log and Diff Commands for Code Review 
On this page:
  • Soft Reset
  • Mixed Reset (Default)
  • Hard Reset
  • Revert a Commit
  • Unstage a File
  • Undo a Commit but Keep Changes
  • Undo a Commit and Discard Changes
  • Amend the Last Commit
  • Restore a Deleted File
  • Restore to a Specific Commit
Copyright © 2025 WE CODE NOW All rights reserved.
WE CODE NOW
Link copied to clipboard
WE CODE NOW
Code copied to clipboard