Remove a commit from a Git branch

Choose reset, interactive rebase, or revert based on the commit position and whether other people depend on the history.

Best practices
Illustration of Git branches and version history

For an unshared latest commit, reset can move the branch back. For an older unshared commit, interactive rebase can omit it. For a commit already used by collaborators, revert usually provides the appropriate history-preserving change.

Before either rewrite, inspect git status and save a backup branch. A branch preserves commits, not uncommitted edits; save those separately.

Remove the latest commit

Choose one reset mode:

# Preserve the index and working files:
git reset --soft HEAD~1
# Or unstage changes while preserving working files:
git reset --mixed HEAD~1
# Or discard local tracked changes and restore the parent tree:
git reset --hard HEAD~1

Soft reset leaves the index exactly as it was, so existing staged changes remain mixed with the removed commit's changes. Mixed reset resets the index to the target. Hard reset also overwrites the working tree and can remove untracked paths that obstruct tracked files.

HEAD~3 follows three first-parent steps. In merge histories, moving there can remove more than three commits from this branch's reachable history. A root commit has no HEAD~1.

Omit an older commit

git rebase -i 'a1b2c3d^'

Change the target entry to drop. Git replays the remaining commits, which can conflict or become empty if they depend on the removed change. Resolve and continue, or abort the rebase. Use --root when the selected commit has no parent; inspect merge topology before using ordinary rebase, which does not preserve merges by default.

Reverse a published change

git revert a1b2c3d

This adds a commit that reverses the selected change without removing the old commit from history. It can still conflict or require application-level follow-up. Reverting a merge requires selecting a mainline parent and understanding future merge behavior.

A coordinated rewrite of published history is technically possible, but requires explicit agreement and may be blocked by branch protection. A force-with-lease check protects against an unexpected remote tip; it does not automatically make the rewrite safe for downstream work.

Recover a mistaken rewrite

git reflog
git branch recovery OLD_COMMIT_ID

Creating a recovery branch lets you inspect the old history without overwriting current work. Recovery depends on retained reflog entries and objects; uncommitted content overwritten by a hard reset is a different problem.

Deleting a commit from this branch does not erase it from other branches, clones, tags, or backups. For exposed credentials, remove access to the secret and follow a dedicated history-cleanup process rather than treating reset as secure erasure.

See git-reset, git-rebase, and git-revert.

Get a tailored plan

Contact us

Join the community

Scan with WeChat
to join the community

Try Guance

Start online and pay only for what you use.

Get started

Choose a Guance plan

Code hosting