Remove a git commit which has not been pushed
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
When working with Git, a distributed version control system, you might sometimes need to undo a commit that has not yet been pushed to a remote repository. This can be necessary for various reasons, like committing the wrong files, committing to the wrong branch, or simply needing to make additional changes. Here’s a guide on how to safely remove a commit in Git without affecting your remote repository.
Understanding Git Commits
Before delving into how to remove a commit, it's important to understand what a commit is within the context of Git. A commit in Git represents a snapshot of your project's history at a particular point in time. Each commit has a unique identifier called a SHA hash, which helps Git keep track of changes.
Checking Your Commit History
First, you’ll want to see the commits to identify which commit needs to be removed. You can view the commit history by using:
This command lists all the commits along with details like the commit hash, author, date, and commit message.
Methods to Remove a Git Commit
1. Using git reset
The git reset command is one of the most common ways to undo local commits. Here's how you can use it:
Soft Reset: A soft reset will undo the commit but keep all changes in your working directory.
Hard Reset: A hard reset will undo the commit and all changes in your working directory, reverting to the state of the previous commit.
2. Using git revert
If you prefer to revert the changes made by the last commit instead of erasing it, you can use the git revert command. This command adds a new commit that undoes the changes of the previous commit.
When to Use Each Method
- Reset: When you need the commit completely removed from your project history, and it has not been shared with others.
- Revert: When you want to keep the record of a change, but negate its effects. This is particularly useful in a shared environment.
Best Practices
- Always make sure you are on the correct branch before resetting or reverting commits.
- Use
git logor other tools likegitkto visually inspect the commit history before making changes. - Consider using
git reset --softfirst to ensure you don’t accidentally lose code.
Summary Table
| Command | Usage | Impact | Safe to Use Before Push? |
git reset --soft HEAD~1 | Undo last commit but keep changes | Does not affect working directory contents | Yes |
git reset --hard HEAD~1 | Undo last commit and discard changes | Affects working directory, resets it to last commit | Yes |
git revert HEAD | Create new commit to undo last commit changes | Adds a new commit, safe for shared branches | Yes |
Final Thoughts
Understanding how to effectively manage your Git history with commands like git reset and git revert is critical for maintaining a clean and functional repository. Always ensure you understand the consequences of using these commands, especially in collaborative environments. Git allows great flexibility in managing history, but with great power comes great responsibility.

