
Reset in Git
🚀 Reset in Git: How to Undo Changes
In Git, reset
is used to undo changes at different levels—unstaging files, removing commits, or resetting your working directory.
📌 Types of git reset
and Their Uses
1️⃣ Soft Reset (--soft
)
✅ Keeps your changes & moves HEAD to a previous commit
✅ Use when you want to change commit history but keep changes
git reset --soft HEAD~1
➡ Moves HEAD back one commit but keeps your changes staged.
➡ Now, you can edit and recommit:
git commit -m "Updated commit message"
2️⃣ Mixed Reset (Default)
✅ Unstages changes but keeps the files
✅ Use when you want to undo a commit but keep the changes
git reset HEAD~1
➡ Moves HEAD back one commit
➡ Your changes remain unstaged, so you can add them again with:
git add .git commit -m "New commit message"
3️⃣ Hard Reset (--hard
)
🚨 WARNING: This permanently deletes commits and changes!
✅ Use when you want to remove everything, including uncommitted changes
git reset --hard HEAD~1
➡ Moves HEAD back one commit
➡ Deletes all changes permanently
🔥 Reset to a Specific Commit
Instead of HEAD~1
, you can reset to a specific commit:
git reset --hard <commit-hash>
Find commit hashes with:
git log --oneline
🎯 Summary
Command | Effect |
---|---|
git reset --soft HEAD~1 | Undo commit but keep changes staged |
git reset HEAD~1 | Undo commit & unstage changes |
git reset --hard HEAD~1 | Deletes commit & changes 🚨 |