Sub topic 8
Removing commits from the middle of a branch
This chapter assumes you can read git log, know that a branch is a pointer to a commit, and have read the earlier chapters on the upstream..branch range and the three-argument form git rebase --onto <newbase> <upstream> <branch>. Here you use that form to delete a run of commits from the middle of a branch, without opening an editor.
Why --onto can delete commits
git rebase --onto does two things. It selects the commits reachable from <branch> but not from <upstream>, which is the range upstream..branch. Then it copies those commits, in order, on top of <newbase>.
Nothing requires <newbase> to be an ancestor of <upstream>, or to be near it. If <newbase> is older than <upstream> on the same line of history, the commits between the two are not in the range and are not copied. After Git moves the branch pointer to the copies, those commits are no longer reachable from the branch. This is how a deletion happens.
The rule has two parts:
- Set
<newbase>to the last commit you want to keep before the gap. - Set
<upstream>to the last commit you want to remove.
Everything after <upstream> is replayed on top of <newbase>. Everything between them is dropped.
A worked example
Suppose a branch called topicA has this history, oldest to newest:
E---F---G---H---I---J topicAYou want to remove F and G and keep H, I, and J on top of E. Count back from the branch tip using the ~ suffix, where topicA~1 is the parent of the tip:
| Name | Commit |
|---|---|
| `topicA` | J |
| `topicA~1` | I |
| `topicA~2` | H |
| `topicA~3` | G |
| `topicA~4` | F |
| `topicA~5` | E |
The command is:
git rebase --onto topicA~5 topicA~3 topicARead the arguments in order:
topicA~5isE, the new base. This is the last commit you keep before the gap.topicA~3isG, the upstream. The rangeG..topicAisH,I,J.topicAis the branch to move. Git switches to it first and rewrites it.
The result is:
E---H'---I'---J' topicAThe prime marks (') show that these are new commits. They hold the same changes and messages as H, I, and J, but each has a new parent, so each has a new hash. F, G, and the old H, I, J still exist in the object database, but no branch points at them.
Try it in a scratch repository
This script builds the history above, with one file per commit so that no conflicts occur. It needs Git 2.28 or later for git init -b.
git init -b topicA demo
cd demo
for c in E F G H I J; do
echo "$c" > "$c.txt"
git add "$c.txt"
git commit -m "$c"
done
# Preview the commits that will be replayed.
git log --oneline topicA~3..topicA
# Remove F and G.
git rebase --onto topicA~5 topicA~3 topicA
# Check the result.
git log --oneline
lsThe preview prints J, I, and H. The final git log prints J, I, H, E, with new hashes for the first three. ls shows E.txt, H.txt, I.txt, and J.txt. The files F.txt and G.txt are gone, because the commits that added them are no longer in the history.
Preview before you rewrite
The range upstream..branch is the list of commits that survive. Anything on the branch that is outside that range and above <newbase> is dropped. So you check two things:
# What will be kept (replayed):
git log --oneline topicA~3..topicA
# What will be removed:
git log --oneline topicA~5..topicA~3The second command prints G and F. If that list contains a commit you want to keep, the arguments are wrong. Fix them before running the rebase.
After the rebase, confirm that the removed commits are really gone and that the other commits are unchanged:
git log --oneline --graph --decorate topicA
git diff ORIG_HEAD topicAORIG_HEAD is the old branch tip. The diff between the old and new tips should show exactly the changes that F and G introduced, reversed. If it shows anything else, something went wrong, and the recovery section below tells you how to go back.
Removing a single commit
To drop one commit X, use its parent as the new base and X itself as the upstream:
git rebase --onto X~1 X topicAFor the example history, removing only G looks like this. G is topicA~3, so G~1 is topicA~4:
git rebase --onto topicA~4 topicA~3 topicAThe new base is F, the range is G..topicA (H, I, J), and the result is E---F---H'---I'---J'.
Using hashes instead of ~ counts is safer when the branch is long. Counting is easy to get wrong by one.
git log --oneline # find the hash of the first commit to remove
git rebase --onto abc1234~1 abc1234 topicAThis removes abc1234 and any commits between it and the tip that you do not want. Wait: it removes only abc1234, and keeps everything after it. To remove a range, the upstream must be the newest commit of the range, not the oldest.
What can go wrong
Conflicts from dependent commits
Dropping a commit does not check whether later commits depend on it. If H edits a line that F added, then replaying H onto E has no F to build on. Git stops with a conflict, or in the worst case applies the change in the wrong place without any conflict.
When it stops, the normal tools apply:
git status # see the conflicted files
# edit the files to resolve the conflict markers
git add path/to/file
git rebase --continue
# Or give up and return to the state before the rebase:
git rebase --abortIf a replayed commit makes sense only with a removed commit, you can skip it with git rebase --skip. That drops that one commit too, so later commits may then conflict as well. Run the project's tests after any rebase that dropped code, because a clean replay does not prove the result still builds.
Merge commits in the range
By default, rebase does not recreate merge commits. It flattens the range into a straight line of the non-merge commits. If the commits after your gap include a merge of another branch, the result can differ from what you expect. Use --rebase-merges to keep the merge structure, and check the result with git log --graph.
Commits that become empty
If a replayed commit makes changes that already exist in the new base, Git drops it as redundant. This rarely matters when removing commits, but it can happen if a later commit repeated the same change.
Dropping commits at the tip
If the commits you want to remove are the newest ones, you do not need --onto. Move the branch pointer back instead:
git reset --hard topicA~2This discards the last two commits. --onto earns its place when the commits to remove have commits you want to keep after them.
Alternatives and when to use them
| Approach | Command | Best for |
|---|---|---|
| `--onto` | `git rebase --onto E G topicA` | Removing a known range from the middle, from the command line |
| Interactive rebase | `git rebase -i E` then mark lines `drop` | Removing scattered commits, or also reordering and editing |
| Revert | `git revert F G` | Branches that others already use, because history is not rewritten |
--onto and git rebase -i both rewrite history. Use revert when the commits are already on a shared branch such as main. It adds new commits that undo the old ones and leaves existing hashes alone.
Undoing and publishing
Because the rebase copies commits, the originals stay in the repository until Git cleans up unreachable objects, which takes weeks by default. To undo a finished rebase:
git reset --hard ORIG_HEAD
# If ORIG_HEAD has been overwritten, find the old tip in the reflog.
git reflog
git reset --hard topicA@{1}If you already pushed topicA, the rewritten branch no longer descends from the remote one, so a normal push is rejected. Push with a lease:
git push --force-with-lease origin topicA--force-with-lease refuses to overwrite remote commits that you have not fetched, so you do not wipe out a teammate's push. Do this only on branches that nobody else builds on. If others have based work on the commits you removed, tell them first, because their branches will still contain F and G.
Summary of the rule
<newbase>is the last commit you keep before the gap.<upstream>is the last commit you remove.<branch>is the branch to rewrite.- Preview
upstream..branchfirst, then verify withgit logandgit diff ORIG_HEAD.
Exercise
A branch work has the history A---B---C---D---E---F with F at the tip. You want to remove only C and D and keep E and F. Write the git rebase --onto command using ~ notation, and say which command you would run first to preview what will be kept.
Show answer
git rebase --onto work~4 work~2 work. The tip F is work, so E is work~1, D is work~2, C is work~3, and B is work~4. The new base is B (work~4), the last commit kept before the gap. The upstream is D (work~2), the last commit removed. The preview is git log --oneline work~2..work, which should list only F and E. The result is A---B---E'---F'.
Exercise
You run git rebase --onto topicA~5 topicA~3 topicA on the example history, then notice that commit H also needed F, and the rebase stopped with a conflict. List two ways to proceed and the command for each.
Show answer
To stop and return the branch to its original state, run git rebase --abort. To continue, edit the conflicted files to resolve the markers, run git add on them, and run git rebase --continue. A third option is git rebase --skip, which drops H as well. If the rebase had already finished and you regret it, git reset --hard ORIG_HEAD restores the old tip.