Git: Reset last commit
git reset --soft, git reset --hard & git revert
When working with Git, it is possible that you committed some code by mistake.
You can use git revert or git reset commands to have a cleaner Git history.

Git revert
If you want to undo a commit without losing the history but creating a new commit undoing what you did, consider using git revert:
git revert HEAD
This command is a safer option in shared repositories.
Git reset
The git reset command moves the HEAD pointer to a previous point, removing intermediate commits.
Git reset soft
After running this command, the changes from the last commit will still be staged.
git reset --soft HEAD~1
Use cases:
- You made a commit but realized you need to modify something before finalizing it
- You want to combine multiple commits into one before pushing
Git reset hard
After running this command, the changes from the last commit will be removed.
git reset --hard HEAD~1
Use cases:
- You want to remove some commits and return to a clean state completely
- You accidentally committed something and need to reset without keeping any modifications
Git push origin
What if you pushed some commits into a remote branch and needed to push your latest changes regardless?
In that case, you can force the push using the flag -f. This command will override the history, which means the previous commits will be removed.
git push origin {branch} -f
Conclusion
Use --soft when you need to adjust your commits without losing changes and --hard when you need a complete reset.
Always double-check before using --hard to avoid unintended data loss!
If you have doubts, or you are working in a branch with other people, it is preferable to use git revert instead.