Git Reset
Definition
Git Reset is a command used to move the current branch pointer backward to a specific commit, effectively undoing changes. It can be used to alter the commit history and manage the state of your working directory or staging area.
Where you hear it
- In code reviews when someone needs to clean up their local commits - During debugging sessions to undo accidental changes - When cleaning up messy commit history before pushing to a shared repository
Examples
Run
git reset --soft HEAD~1to undo the last commit while keeping your changes in the staging area.Use
git reset --hard HEAD~1to completely erase the last commit and all your uncommitted work.I used git reset to drop the three commits I made on the wrong branch.
Common mistake
Using --hard without realizing it permanently deletes uncommitted changes and working directory modifications, making them very difficult to recover.
Don't confuse with
Git Reset moves the branch pointer and alters history, whereas Git Revert creates a new commit that undoes previous changes without rewriting history.
Say it at work
- In a meeting or chat
Let's run a soft git reset on that branch to clean up the last few commits before we merge.
- In an email or ticket
Please avoid using git reset --hard on shared branches because it will break other developers' local history.
Keep exploring
More in Git & Collaboration