Git Rebase Drop Commit - Remove a Commit from History with drop, Compared with git revert, with Real Output for the Conflict, the Silent sed Typo and the Force-Push Rejection (git 2.43.0)
TL;DR
git rebase -i <base>, then changepicktodropon the line of the commit you want gone. Git replays the remaining commits on top of<base>without it. The git rebase reference describesdropas: the commit will be dropped.- Every commit after the dropped one gets a new hash, because its parent changed. In my run the dropped commit's successor went from
e18bde9to2c438d9. - A drop can conflict. If a later commit builds on lines the dropped commit added, the rebase stops with
Could not apply <hash>... <subject>, and you either resolve it or rungit rebase --abort. - Undo a drop with
git reset --hard ORIG_HEADright after the rebase, or find the old tip ingit reflog(the git gc reference says reflog entries for commits no longer reachable from the tip expire after 30 days by default, reachable ones after 90 days). - On a shared branch use
git revertinstead. It adds a new commit that undoes the change, so nobody's history is rewritten, but the bad commit stays in the log. - Not your exact case? To squash several commits into one rather than remove one, read Git Squash Commits; to change only the last commit, read Git Commit Amend.
Table of Content
- What drop does and when to use it
- The lab repository
- Dropping a commit without opening an editor
- Undoing a drop
- git revert for comparison
- When the drop stops with a conflict
- Pushing a rewritten branch
- Deleting the line instead of writing drop
- What broke when I ran this
- Conclusion
1. What drop does and when to use it
Interactive rebase gives you a list of commits, one per line, each starting with a command. The default command is pick, which keeps the commit. Change it to drop and git leaves that commit out when it rebuilds the branch. That is the whole feature, and it is the cleanest way to delete a commit that sits in the middle of your history, because git reset can only remove commits from the tip.
The choice between drop and revert is a question about who else has the commits.
git rebase -i with drop | git revert <commit> | |
|---|---|---|
| History | rewritten, the commit disappears from the branch | preserved, a new commit is added |
| Hashes of later commits | all change | unchanged |
| Push after | needs --force-with-lease if the commits were already pushed | normal push |
| Secret or large file in the commit | gone from the branch, still in the reflog for a while | stays in the log, anyone can read it |
| Can conflict | yes, when later commits depend on it | yes, when later commits touch the same lines |
| Good for | your own unpushed or feature-branch commits | shared branches such as main |
The git revert reference says it records new commits that reverse the effect of earlier ones, and requires a clean working tree.
If you are not yet comfortable with what a commit is underneath, What Is Git shows the objects that drop leaves out and rewrites.
2. The lab repository
I ran everything on 2026-12-04 with git 2.43.0. The repository has four commits. The second one is the kind of commit I would want to remove: a debug dump that I committed by accident and that contains a password.
1$ date -u +%F; git --version
22026-12-04
3git version 2.43.0
4$ git log --oneline
5c918f7f feature b
623e1791 feature a
7bb9a85c temp: add debug dump
807df1be add app
Each of the later commits only appends a line to app.txt, and the debug commit only adds a new file debug.txt. That matters in section 6: when the dropped commit touches different files from its neighbours there is nothing to conflict, and when it does not, there is.
3. Dropping a commit without opening an editor
Normally git rebase -i opens your editor on the instruction list and you type drop by hand. For a copy-paste command, and so the output is reproducible, I replaced the editor with a sed command through the GIT_SEQUENCE_EDITOR variable. The sed turns the pick line whose subject matches into a drop line:
1$ GIT_SEQUENCE_EDITOR="sed -i 's/^pick \(.*\) temp: add debug dump/drop \1 temp: add debug dump/'" git rebase -i --root
2Rebasing (4/4)Successfully rebased and updated refs/heads/main.
3$ git log --oneline
45e40d56 feature b
52e5e847 feature a
607df1be add app
7$ ls
8app.txt
Three things to read in that output. The debug.txt file is gone from the working tree. The two commits after the dropped one now have different hashes (c918f7f became 5e40d56, 23e1791 became 2e5e847) while 07df1be, which comes before it, kept its hash. And --root was needed only because the bad commit was the second commit in the repository; in normal work you pass a base such as HEAD~3 or origin/main.
If you prefer the editor, run git rebase -i HEAD~3, change the word pick to drop (or the single letter d) on the line, save and close. Same result.
How far back should the base go
The base is the commit before the one you want to remove. git rebase -i HEAD~2 shows the last two commits. In a second run on a three-commit repository I dropped the debug commit with HEAD~2, and the reflog recorded exactly what happened:
1$ git reflog | head -4
22c438d9 HEAD@{0}: rebase (finish): returning to refs/heads/main
32c438d9 HEAD@{1}: rebase (pick): feature a
41431c25 HEAD@{2}: rebase (start): checkout HEAD~2
5e18bde9 HEAD@{3}: commit: feature a
Only one pick line appears because the dropped commit was never replayed. e18bde9 at HEAD@{3} is the old tip, and that is what makes the undo in the next section possible.
The cost grows with the distance, not with the repository
Every commit after the dropped one is replayed, so the base decides how long the rebase takes. I timed it in a 1,001-commit repository where each commit adds its own file: dropping the oldest commit of the last 100 (HEAD~100) took 7,393 ms on git 2.43.0, which is roughly 75 ms per replayed commit, and the branch ended with 1,000 commits. Pick the base as close to the bad commit as you can. I did not wait for the same run over 1,000 commits to finish, so I have no number for it.
4. Undoing a drop
Immediately after a rebase, git stores the previous tip in ORIG_HEAD. So if you dropped the wrong commit, one command brings everything back:
1$ git reset -q --hard ORIG_HEAD
2$ git log --oneline
3e18bde9 feature a
49ea9cd8 temp: add debug dump
51431c25 add app
6$ ls
7app.txt
8debug.txt
Note that the old hashes (e18bde9, 9ea9cd8) are back, not new copies. The commits were never deleted from the object store; the branch pointer just moved away from them and then moved back. After other commands overwrite ORIG_HEAD, use git reflog and reset to the entry you want. I also wrote this recovery up with more cases in Git Squash Commits.
The flip side matters if the commit contained a secret: dropping it does not make the secret disappear. The old commit stays readable through the reflog until the entry expires, and the git gc reference sets the default for entries no longer reachable from the branch tip, which is what a dropped commit is, at 30 days (gc.reflogExpireUnreachable), and 90 days for reachable ones (gc.reflogExpire). Until then, git show <hash> still prints it; for the entries, see the git reflog reference. If a password was already pushed, assume it is compromised and rotate it; rewriting history is not a substitute.
5. git revert for comparison
Starting again from that restored state, here is the other way to get rid of the debug commit:
1$ git revert --no-edit HEAD~1
2[main e53a715] Revert "temp: add debug dump"
3$ git log --oneline
4e53a715 Revert "temp: add debug dump"
5e18bde9 feature a
69ea9cd8 temp: add debug dump
71431c25 add app
8$ ls
9app.txt
The working tree ends up the same as with drop, since debug.txt is gone, but the log now has four commits in place of three, and the original debug commit is still there for anyone to read with git show 9ea9cd8. That is the right trade on a shared branch: teammates who already pulled 9ea9cd8 can pull the revert with an ordinary git pull, with nothing to repair.
My rule is simple. If the commit exists only on my machine or on a feature branch only I push to, I drop it. If it is on main or any branch other people have pulled, I revert it.
6. When the drop stops with a conflict
Dropping is easy when the commit is independent. It stops when a later commit depends on it. I made a three-commit file where add b appends the line b and add c appends c right after it, then dropped add b:
1$ GIT_SEQUENCE_EDITOR="sed -i 's/^pick \(.*\) add b/drop \1 add b/'" git rebase -i HEAD~2
2hint: "git add/rm <conflicted_files>", then run "git rebase --continue".
3hint: You can instead skip this commit: run "git rebase --skip".
4hint: To abort and get back to the state before "git rebase", run "git rebase --abort".
5Could not apply 687eef4... add c
6$ git status -sb | head -4
7## HEAD (no branch)
8UU f.txt
9$ cat f.txt
10a
11<<<<<<< HEAD
12=======
13b
14c
15>>>>>>> 687eef4 (add c)
Git cannot apply add c cleanly because its patch assumed the line b was already there. The conflict markers show the situation: HEAD (the rebuilt branch so far) has nothing after a, while the commit being replayed wants b and c. You have two honest options:
- Resolve it. Edit
f.txtto what you want the final file to say (probablyaandc),git add f.txt,git rebase --continue. - Back out.
git rebase --abortreturns the branch to exactly where it was. I ran it, andgit log --onelineshowed687eef4 add c,b258e15 add b,8fb8e7e base, the original three commits with their original hashes.
If you hit this often, the real problem is usually that the commit you want to remove is not self-contained. Splitting commits earlier (one concern per commit) makes later drops painless.
7. Pushing a rewritten branch
A drop changes hashes, so a branch that was already pushed no longer fast-forwards. I cloned from a local bare repository, pushed three commits, dropped the middle one (temp: debug dump, containing token=hunter2) with HEAD~2, and tried a normal push:
1$ git push origin main
2 ! [rejected] main -> main (non-fast-forward)
3hint: Updates were rejected because the tip of your current branch is behind
4$ git push --force-with-lease origin main
5 + 581f8a2...3746443 main -> main (forced update)
I use --force-with-lease rather than --force because it refuses to overwrite the remote if someone else pushed in the meantime. Details of that, including a case where it still deletes a teammate's work, are in Git Commit Amend. The git push reference documents the option.
Do not do this on a branch other people are working from. Tell them first, or use revert.
8. Deleting the line instead of writing drop
In the editor you can also delete the commit's line entirely, and for a long time that was the only way. It works, but it is easy to delete the wrong line by accident, and git has a guard for that: the rebase.missingCommitsCheck setting. The git config reference says that with warn git prints a warning if a line was removed but proceeds, with error it stops the rebase, and with ignore, the default, it does nothing. To drop a commit without any warning, use the drop command.
I set it to error and deleted the line for commit 2:
1$ git config rebase.missingCommitsCheck error
2$ GIT_SEQUENCE_EDITOR="sed -i '/commit 2/d'" git rebase -i HEAD~2
3Warning: some commits may have been dropped accidentally.
4Dropped commits (newer to older):
5 - 015be9c commit 2
6To avoid this message, use "drop" to explicitly remove a commit.
7
8Use 'git config rebase.missingCommitsCheck' to change the level of warnings.
9The possible behaviours are: ignore, warn, error.
The rebase stopped, and git status showed HEAD (no branch) until I ran git rebase --abort. I now keep error on globally (git config --global rebase.missingCommitsCheck error) and always write drop when I mean it. That turns an accidental deleted line into a stop instead of silent history loss.
9. What broke when I ran this
A sed typo dropped nothing and git still said success. When I scripted the drop I misspelled the subject in the pattern (temp: debug dumpp). The sed matched no line, every commit stayed a pick, and git did exactly what it was asked:
1$ GIT_SEQUENCE_EDITOR="sed -i 's/^pick \(.*\) temp: debug dumpp/drop \1 x/'" git rebase -i HEAD~2
2Successfully rebased and updated refs/heads/main.
3$ git log --oneline | head -3
4581f8a2 feature a
593405ab temp: debug dump
6e7ccb99 add app
The debug commit was still there, and the message gave no hint. Always run git log --oneline afterwards and check that the commit is actually gone, especially in scripts. The fix is to read the log after the rebase, or to use the editor where you see the list.
A drop that conflicted, which I had not expected. I assumed removing a commit that only appends a line would be trivial. It was not, because the commit after it used that line as context (section 6). The error text to search for is Could not apply 687eef4... add c. The fix is to resolve and --continue, or --abort.
The push after the drop was rejected with non-fast-forward (section 7). That is not a bug; it is the proof that history was rewritten. The fix is --force-with-lease, and only on a branch that is yours.
What I did not test: dropping a merge commit. Git's rebase flattens merges unless you pass --rebase-merges, which is a different post.
10. Conclusion
git rebase -i <base>anddropremoves a commit from your branch; every later commit gets a new hash.git reset --hard ORIG_HEADundoes it right after, and the reflog covers the rest.- Use
git revertfor shared branches; usedropfor your own. - Expect a conflict when later commits depend on the dropped one, and
--abortif you do not want to deal with it. - Set
rebase.missingCommitsChecktoerrorso a deleted line cannot silently remove history, and always readgit log --onelineafterwards. - Dropping a commit does not remove a leaked secret; rotate it.
This is one lesson of the DevOps Master Course on YouTube, and the written foundation is in the free Git and GitHub blueprint course. If you want to test every commit rather than remove one, see git rebase --exec.
Did this save you a debugging session? Stay in the loop -
- Subscribe on YouTube - every post here has a screen-recorded companion video.
- Code on GitHub - the repositories, Terraform and scripts behind these posts.