Git Commands
Version Control
Remote Branch
Commit Removal
Coding Best Practices

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.

Browse interview questions

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 --hard when 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:

bash
1git checkout feature-branch
2git log --oneline -n 15
3
4git branch backup/feature-branch-before-cleanup
5
6# move branch head to the last good commit
7git reset --hard HASH_TO_KEEP
8
9git push --force-with-lease origin feature-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:

bash
git checkout feature-branch
git branch backup/feature-branch-before-cleanup
git rebase -i BASE_COMMIT

In the editor, mark unwanted commits as drop. Save and exit, then resolve conflicts if prompted.

After rebase succeeds:

bash
git push --force-with-lease origin feature-branch

Finally inspect remote history:

bash
git fetch origin
git log --oneline origin/feature-branch -n 20

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:

bash
git fetch origin
git checkout feature-branch
git reset --hard origin/feature-branch

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:

  1. rotate leaked keys immediately
  2. invalidate affected tokens
  3. review logs for misuse
  4. 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:

bash
git fetch --all --prune
git branch -r --contains BAD_COMMIT_HASH

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:

bash
git checkout feature-branch
git reset --hard backup/feature-branch-before-cleanup
git push --force-with-lease origin feature-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 --force and 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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.