February 20, 2025
O. Wolfson
git reset is a powerful command in Git that allows developers to undo changes in their repositories. It is often used to move the current HEAD to a specified state, enabling developers to modify or discard certain commits. Understanding git reset is crucial for effective version control and maintaining clean project histories.
git resetgit reset primarily operates on three distinct areas within a Git repository:
The behavior of git reset depends on the mode used:
git resetgit reset --soft <commit><commit>.git reset --mixed <commit> (default)<commit>.<commit>.git reset --hard <commit><commit>.<commit>.<commit>.bash# Keep changes staged
git reset --soft HEAD~1
# Unstage changes but keep modifications
git reset --mixed HEAD~1
# Discard changes permanently
git reset --hard HEAD~1
bashgit reset --soft <commit-hash> git reset --mixed <commit-hash> git reset --hard <commit-hash>
git reset and git revert| Command | Purpose | Data Loss | Safe for Shared Branches |
|---|---|---|---|
git reset | Moves HEAD and modifies history | Potential | No |
git revert | Creates a new commit to reverse changes | No | Yes |
--hard carefully as it can permanently delete changes.git reset on public/shared branches to prevent rewriting history that others rely on.git reset We’ll walk through creating a simple file, making some commits, and performing various git reset operations to see the actual effect.
Open your terminal and follow these steps:
bashmkdir git-reset-demo
cd git-reset-demo
git init
This creates an empty Git repository.
file.txt and Make Initial Commitbashecho "Line 1" > file.txt
git add file.txt
git commit -m "Add Line 1"
bashecho "Line 2" >> file.txt
git add file.txt
git commit -m "Add Line 2"
bashecho "Line 3" >> file.txt
git add file.txt
git commit -m "Add Line 3"
Check the history:
bashgit log --oneline
Example output:
bashabcd123 Add Line 3 efgh456 Add Line 2 ijkl789 Add Line 1
(Your commit hashes will differ.)
git reset OperationsWe’ll demonstrate each type of reset using the third commit (HEAD) as the starting point.
git reset --soft: Undo Commit, Keep Changes StagedRun:
bashgit reset --soft HEAD~1
Observe the Result:
bashgit status
Check the Contents:
bashcat file.txt
Output:
bashLine 1 Line 2 Line 3
Recommit or Modify:
bashgit commit -m "Revised Line 3"
git reset --mixed: Undo Commit, Keep Changes in Working DirectoryRun:
bashgit reset --mixed HEAD~1
Observe the Result:
bashgit status
Check the File:
bashcat file.txt
Output:
bashLine 1 Line 2 Line 3
Stage and Commit Again if Needed:
bashgit add file.txt
git commit -m "Recommit Line 3"
git reset --hard: Undo Commit and Discard Changes (DANGEROUS)Run:
bashgit reset --hard HEAD~1
Observe the Result:
Check the File:
bashcat file.txt
Output:
bashLine 1 Line 2
Line 3 is GONE!
⚠️ Be Careful: This cannot be undone easily.
Assuming your commit history looks like this:
bashabcd123 Add Line 3 efgh456 Add Line 2 ijkl789 Add Line 1
efgh456 (Second Commit)bashgit reset --mixed efgh456
Check the file:
bashcat file.txt
Use these at any step to understand what’s happening:
bashgit log --oneline
git status
cat file.txt
| Command | Result |
|---|---|
git reset --soft HEAD~1 | Undo commit, keep changes staged |
git reset --mixed HEAD~1 (default) | Undo commit, keep changes unstaged |
git reset --hard HEAD~1 | Undo commit, discard changes in working directory and staging |
git reset is a versatile tool for managing commits, the index, and the working directory. Mastering its different modes enables developers to effectively control their project history and recover from mistakes. Understanding when to use git reset versus git revert is vital to ensuring smooth collaboration and preventing data loss in version-controlled projects.