Introduction
When you’re working on a project, it’s common to realize that a commit doesn’t quite fit the desired direction. Whether you’ve pushed the wrong changes, introduced a bug, or simply want to clean up the history before sharing with teammates, knowing how to remove commits from a local branch is an essential Git skill. This guide walks you through the most reliable techniques—git reset, git revert, interactive git rebase, and the hidden power of reflog—so you can keep your repository tidy and your codebase healthy.
Understanding Git Commits and Branches
Git stores changes as a series of snapshots called commits, linked together in a directed acyclic graph (DAG). Each commit points to its parent, forming a linear or branching history. A branch is simply a pointer to a specific commit; when you create a new branch, Git creates a new reference that starts at the current commit.
Because commits are immutable, you cannot directly “delete” them from the history without altering the pointers that reference them. The methods described below manipulate these pointers—HEAD, the branch tip, and sometimes the reflog—to achieve the effect of removing unwanted commits from your working branch.
Methods to Remove Commits from a Local Branch
1. Using git reset
git reset changes the position of the branch tip, effectively discarding commits that fall behind the new tip. There are three modes: soft, mixed (default), and hard Most people skip this — try not to..
When to use:
- You want to undo commits without losing staged changes.
- You’re comfortable discarding changes from the working directory (use hard with caution).
Steps:
- Identify the commit to reset to – usually the commit before the unwanted ones.
- Run the command:
git reset --soft# keeps staged changes git reset --mixed # default, unstages changes git reset --hard # discards all changes in working tree - The branch now points to the chosen commit; the removed commits remain in the repository’s object database but are no longer reachable from any branch.
Example:
If you have commits A → B → C → D and you want to remove C and D, reset to B:
git reset --mixed B
The branch now points to B, and C and D become dangling commits.
2. Using git revert
git revert creates a new commit that undoes the changes introduced by a previous commit. This is a non‑destructive method because it preserves the original commit history while neutralizing its effects.
When to use:
- You need to maintain a clean history for collaborative projects.
- You want to revert changes without rewriting history (important if others have already pulled the commits).
Steps:
- Determine the commit you wish to revert.
- Execute:
git revert - Git opens an editor; you can adjust the commit message or accept the default.
- Save and close the editor to create the revert commit.
Example:
To undo commit C that added a problematic feature:
git revert C
A new commit E appears, containing the inverse of C’s changes.
3. Interactive git rebase
Interactive rebase lets you edit, drop, or reorder commits by rewriting the branch’s history. It’s powerful but should be used only on local branches that haven’t been shared Worth keeping that in mind..
When to use:
- You have full control over the branch and want a clean, linear history.
- You’re comfortable with rewriting history because no one else has based work on it.
Steps:
- Start an interactive rebase from the commit you want to keep:
Thegit rebase -i^ ^denotes the parent of the commit you want as the new tip. - In the editor that opens, you’ll see a list of commits. To remove commits, change
picktodrop(ord). - Save and close the editor.
- Git will apply the rebase, discarding the dropped commits.
Example:
For commits A → B → C → D, to drop C and D and keep only A → B:
git rebase -i B^
Edit the todo list to set C and D as drop, then finish Small thing, real impact..
4. Leveraging the Reflog
The reflog (reference log) records every change to HEAD, providing a timeline of where HEAD has been. It’s a safety net that lets you recover lost commits even after they’ve been removed.
When to use:
- You’ve accidentally reset or rebased and need to find the original commit hash.
- You want to verify the state of your branch after a series of operations.
Steps:
- View the reflog:
git reflog - Identify the entry that points to the commit you want to restore.
- Reset HEAD to that commit (if it’s still reachable):
git reset --hard
Example:
If you reset to an earlier commit and later realize you need a dropped commit, the reflog will show the exact hash where HEAD was before the reset. You can then reset back Less friction, more output..
Scientific Explanation: How Git Tracks History
Git’s object model consists of three primary objects: blob (file content), tree (directory structure), and commit (a snapshot). A commit contains the SHA‑1 hash of its parent commit(s), creating a chain.
If you're run git reset, Git updates the branch pointer (e.That's why , refs/heads/feature) to a new commit, making the previous commits dangling. On the flip side, g. They remain in the object database but are no longer referenced, and Git will eventually garbage‑collect them It's one of those things that adds up..
git revert creates a new commit whose tree is the inverse of the target commit’s changes. This new commit points to the previous HEAD, preserving the original history while neutralizing the effect.
Interactive rebase rewrites history by amending commits onto a new base. It uses amend operations under the hood, which rewrite the commit’s SHA‑1 and update the branch pointer accordingly Turns out it matters..
The reflog is stored in refs/heads/* and refs/stash. Each entry is