Sub topic 8
Removing commits from the middle of a branch
This chapter assumes you can read the --onto <newbase> <upstream> <branch> form and know that upstream..branch selects the commits to replay. It also assumes you are comfortable with git log and git rebase --continue.
The idea
git rebase --onto copies the commits in upstream..branch onto newbase. Commits outside that range are not copied. If newbase is an older commit on the same branch, the commits between newbase and upstream are left out of the result. That is how you remove a range of commits from the middle of a branch.
Suppose topicA has this history, oldest first:
E---F---G---H---I---J topicAYou want to remove F and G but keep H, I, and J.
Choosing the three arguments
- New base:
E, the last commit you want to keep before the gap. - Upstream:
G, the last commit you want to remove. Everything after it is kept. - Branch:
topicA.
Count back from the tip to name these commits. J is topicA~0, I is ~1, H is ~2, G is ~3, F is ~4, and E is ~5.
# Remove F and G, keep H, I, J on top of E.
git rebase --onto topicA~5 topicA~3 topicA
# Result: E---H'---I'---J' topicAGit computes topicA~3..topicA, which is H, I, and J. It switches to topicA~5 (commit E) and replays those three commits there. Then it moves the topicA pointer to the last copy. F and G are no longer reachable from the branch.
The primes mark new commits. They have the same changes and messages as the originals but different parents, so they have different IDs.
Check the range first
Because the upstream decides what is kept, an off-by-one mistake drops the wrong commit. Preview both sides before you run the rebase.
# These commits will be kept (replayed).
git log --oneline topicA~3..topicA
# These commits will be removed.
git log --oneline topicA~5..topicA~3The first command should list H, I, and J. The second should list F and G. If either list is wrong, adjust the ~N numbers before you run the rebase.
After the rebase, check the result:
git log --oneline --graph topicAConflicts can appear
Removing commits does not guarantee a clean replay. If H edits a line that G introduced, Git cannot apply H on top of E. The rebase stops at that commit.
git status # see the conflicted files
# edit the files to resolve the conflict markers
git add path/to/file
git rebase --continueA conflict like this means H depends on a change you are trying to remove. You can resolve it by hand, or you can run git rebase --abort and rethink which commits to drop.
Undoing and publishing
If the result is wrong, ORIG_HEAD still points at the old tip right after the rebase finishes.
git reset --hard ORIG_HEADIf you have since run other commands that overwrite it, look in the reflog with git reflog and reset to the entry from before the rebase.
If topicA was already pushed, the rewritten branch diverges from the remote one. Push it with a lease:
git push --force-with-lease origin topicASummary of the pattern
| Goal | Argument | Value in the example |
|---|---|---|
| Where kept commits go | `<newbase>` | `topicA~5` (E) |
| Last commit to discard | `<upstream>` | `topicA~3` (G) |
| Branch to rewrite | `<branch>` | `topicA` |
The commits between newbase and upstream are the ones removed.
Exercise
A branch named work has the history A---B---C---D---E, with E at the tip. You want to remove C and D but keep E on top of B. Write the git rebase --onto command using ~N notation, and the command that previews the commits that will be kept.
Show answer
git rebase --onto work~3 work~1 work. Here work~3 is B (the new base) and work~1 is D (the upstream, the last commit to remove). Preview the kept commits with git log --oneline work~1..work, which should list only E.