Discarding the Changes (Hard Reset)
If you want to completely remove the commit and all changes associated with it, you can perform a hard reset:
git reset --hard HEAD~1
- `HEAD~1` again refers to the commit immediately before the latest one.
- `–hard` resets your current branch’s HEAD to the previous commit and discards all changes in the working directory and staging area.
Note: Be cautious when using `–hard` as it will permanently delete your uncommitted changes. Always make sure to back up your work before performing a hard reset.
How to Delete Local Commit in Git?
Deleting a local commit in Git can be done in a few ways, depending on whether the commit is the most recent one or if it is further back in the commit history. Here’s an article on how to delete a local commit in Git, tailored to whether you want to keep the changes made in the commit or discard them entirely:
Table of Content
- Keeping the Changes (Soft Reset)
- Discarding the Changes (Hard Reset)
- Handling Pushed Commits
- Deleting a Specific Commit (Using git reflog and cherry-pick)
- Summary