About this question
You can undo the last commit in Git using git reset. Use --soft to keep changes staged, --mixed to unstage them, or --hard to discard them completely.
You can undo the last commit in Git using git reset. Use --soft to keep changes staged, --mixed to unstage them, or --hard to discard them completely.
Log in to share your answer and help other learners.
Log in to answerBest Answer · By JanBask DevOps Expert
Answered on Mar 18, 2025
To undo the most recent local commit in Git, you have a few options depending on what you want to do with the changes.
1. Keep the changes staged (Undo commit but keep files staged)
Use:
git reset --soft HEAD~1This removes the commit but keeps your changes staged, so you can modify and commit them again.
2. Keep the changes but unstage them (Undo commit and unstage files)
Use:
git reset --mixed HEAD~1This removes the commit and unstages your changes, so they remain in your working directory. You’ll need to add them again before committing.
3. Completely remove the commit and changes (Undo commit and delete changes)
Use:
git reset --hard HEAD~1Warning: This will permanently delete your changes from the last commit. Use this only if you’re sure you don’t need them.
4. Undo multiple commits
To undo multiple commits, replace HEAD~1 with the number of commits you want to remove:
git reset --soft HEAD~3 # Undo last 3 commits5. Undo a pushed commit
If you've already pushed the commit, you'll need to force push:
git push origin main --forceFree tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.
Most-read guides across JanBask
Common DevOps interview questions, answered
Guides, tips and career advice on DevOps from JanBask experts.
DevOps How to Become a DevOps Engineers: Learning Path, Critical skills & More
Looking for answer to how to become a DevOps engineer in 2025? Here is the detailed article about DevOps career path, DevOps…
DevOps Top 50+ Jenkins Interview Questions And Answers For 2025
Here are the Top 50 Jenkins interview questions frequently asked in top MNCs which will help you land an ideal job! These…
DevOps Best 37 DevOps Tools To Learn And Master In 2025: Docker, Git & More
Want to know what are Devops tools and their uses? Or looking for the best DevOps list for automation, monitoring, & development?…
DevOps DevOps Engineer Job Description: Roles and Responsibilities [2025 Updated]
Discover the essential roles and responsibilities of a DevOps Engineer by exploring the detailed DevOps job description, and…