How to Delete Local Commits in Git
- Deleting a Single Commit
- Deleting Multiple Commits
- Using Git Rebase to Delete Commits
- Conclusion
- FAQ

Managing your Git repository effectively is crucial for maintaining a clean and organized codebase. Sometimes, you may find yourself needing to delete local commits—whether it’s due to mistakes, unnecessary changes, or simply a change of direction in your project.
In this article, we’ll explore various methods to delete local commits in Git. You’ll learn how to remove a single commit as well as multiple commits, ensuring that you can keep your commit history tidy. Let’s dive in and discover how to manage your commits like a pro!
Deleting a Single Commit
If you’ve made a single commit that you want to undo, Git provides a straightforward way to do this. You can use the git reset
command to remove the commit while keeping your changes in the working directory. This is particularly useful if you want to make modifications before recommitting.
Here’s how you can delete the most recent commit:
git reset HEAD~1
Output:
Unstaged changes after reset:
M filename.ext
This command tells Git to move the current branch pointer back by one commit (HEAD~1). The changes from that commit will remain in your working directory, allowing you to edit them or stage them again as needed. If you want to delete the commit and discard the changes entirely, you can add the --hard
option:
git reset --hard HEAD~1
Output:
HEAD is now at <commit_hash> <commit_message>
Using --hard
will remove the commit and all associated changes, so be cautious with this option. It’s a powerful command that can lead to data loss if you’re not careful. Always ensure you have backups of important changes before using it.
Deleting Multiple Commits
Sometimes, you may need to delete several commits at once. This can be done using the git reset
command as well, but you can specify how many commits to go back. For instance, if you want to delete the last three commits, you would use:
git reset HEAD~3
Output:
Unstaged changes after reset:
M file1.ext
M file2.ext
M file3.ext
This command will remove the last three commits from your history while keeping the changes in your working directory. If you prefer to discard those changes entirely, you can again use the --hard
option:
git reset --hard HEAD~3
Output:
HEAD is now at <commit_hash> <commit_message>
Deleting multiple commits can be risky, especially if those commits were pushed to a shared repository. If you’ve already shared these commits with others, consider using git revert
instead to create new commits that effectively undo the changes made by the previous commits while preserving history.
Using Git Rebase to Delete Commits
Another powerful method to delete commits is using git rebase
. This approach is particularly useful for cleaning up your commit history before merging branches. With interactive rebase, you can choose which commits to keep, edit, or delete.
To start an interactive rebase for the last five commits, you would run:
git rebase -i HEAD~5
Output:
pick <commit_hash> Commit message 1
pick <commit_hash> Commit message 2
pick <commit_hash> Commit message 3
pick <commit_hash> Commit message 4
pick <commit_hash> Commit message 5
This command opens your default text editor with a list of the last five commits. You can change the word pick
to drop
next to any commit you want to delete. After saving and closing the editor, Git will reapply the remaining commits, effectively removing the ones you dropped.
Using interactive rebase allows for a more granular control over your commit history, enabling you to clean it up without losing the context of your changes. However, be careful when using this method, especially if the commits have already been shared with others.
Conclusion
Deleting local commits in Git is a valuable skill that can help you maintain a clean and organized project history. Whether you’re removing a single commit, multiple commits, or using interactive rebase for more control, understanding these methods will empower you as a developer. Always be cautious with commands that can lead to data loss, and consider your team’s workflow when altering shared commit histories. With these techniques in your toolkit, you’ll be able to manage your Git repository more effectively.
FAQ
-
How can I see my commit history in Git?
You can view your commit history by using the commandgit log
. -
What happens if I delete a commit that has been pushed to a remote repository?
If you delete a commit that has been pushed, it can cause issues for others working on the same branch. It’s best to usegit revert
in such cases. -
Can I recover a deleted commit in Git?
Yes, you can recover deleted commits using thegit reflog
command, which shows a log of where your HEAD has been. -
What is the difference between
git reset
andgit revert
?
git reset
moves the branch pointer back, deleting commits, whilegit revert
creates a new commit that undoes the changes of a previous commit without altering history. -
Is it safe to use
git reset --hard
?
Usinggit reset --hard
can be risky as it permanently deletes changes. Always ensure you have backups before using it.
John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.
LinkedInRelated Article - Git Reset
- Difference Between the Git Reset, Revert, and Checkout Commands
- How to Make the Development Branch Identical to the Master Branch
- How to Remove Local Git Changes
- How to Revert a Git Merge With Conflicts
- Difference Between Git RM --Cached and Git Reset File
- How to Revert a Git Repository by Commit ID