How to permanently remove few commits from remote branch
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Removing commits from a remote branch means rewriting history and force-updating the branch reference. This is useful when sensitive data was committed, bad merges landed, or cleanup is required before a release. Because history rewrite changes commit IDs, you need a controlled process that includes backup, coordination, and verification.
Core Sections
Choose the Rewrite Method by Commit Location
Pick the smallest tool that solves the problem:
- use
git reset --hardwhen removing the most recent contiguous commits - use interactive rebase when dropping selected commits from recent history
- use history-rewrite tools for repository-wide secret cleanup
For most branch-level cleanup tasks, reset or rebase is enough.
Case 1: Remove the Last Few Commits
If the bad commits are at the tip of your branch:
--force-with-lease is safer than plain --force because it refuses to overwrite remote changes you have not fetched.
Case 2: Drop Specific Commits with Interactive Rebase
If you need to remove selected commits in the middle of recent history:
In the editor, mark unwanted commits as drop. Save and exit, then resolve conflicts if prompted.
After rebase succeeds:
Finally inspect remote history:
Coordinate with Collaborators Before Force Push
History rewrite on shared branches affects everyone tracking that branch. Announce the rewrite window and provide recovery commands.
Typical teammate recovery:
Without this communication, old local history can be pushed again and reintroduce removed commits.
Secret Exposure Requires Extra Response
If commits contained credentials, rewriting history is only part of the incident response. You should also:
- rotate leaked keys immediately
- invalidate affected tokens
- review logs for misuse
- scan related artifacts such as build outputs and deployment configs
Do not treat Git rewrite alone as complete remediation.
Protected Branches and Governance
Many repositories block force pushes on protected branches. Respect policy and coordinate with repository admins.
Practical options:
- temporary policy change with explicit approval
- perform rewrite on maintenance branch, then update protected branch via approved process
- use emergency playbook if your organization has one
Document who approved the operation and when the branch was rewritten.
Validate That Removed Commits Are Gone
After push, run checks from a fresh fetch:
If the removed commit is still reachable from another branch or tag, history cleanup is incomplete. Decide whether those refs also need updates.
For sensitive cleanup, run your secret scanner again after rewrite to confirm repository state.
Recovery If You Rewrite the Wrong History
If cleanup went too far, restore from your backup branch:
This is why creating a backup reference before rewriting is non-negotiable on active repositories.
Common Pitfalls
- Rewriting remote history without creating a backup branch first.
- Using plain
--forceand accidentally overwriting teammates' new work. - Removing commits that leaked secrets but not rotating the compromised credentials.
- Forgetting that tags or other branches may still reference removed commits.
- Performing force-push cleanup on protected branches without approval and communication.
Summary
- Permanent commit removal requires history rewrite plus force push.
- Use reset for latest commits and interactive rebase for selective drops.
- Always create backup refs and use
--force-with-lease. - Coordinate with collaborators before and after rewriting shared branches.
- For secret incidents, combine Git cleanup with credential rotation and verification scans.
Related reading
- How to permanently remove few commits from remote branch
- How to prefer files from one branch during a merge?
- How to preview the changes that git stash apply will make?
- How to preview the changes that git stash apply will make?
- How to programmatically determine the current checked out Git branch
- How to programmatically determine whether the Git checkout is a tag and if so, what is the tag name
- How to prune local tracking branches that do not exist on remote anymore?
- How to prune local tracking branches that do not exist on remote anymore?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.