tl;dr: A one line command will work: git rebase 3371cec 2f05aba --onto e2b2a84
The Long:
If the one or more commits are grouped together, you can use a one-line rebase command without needing to bother with interactive mode. For example:
# Suppose you have: A-B-X-Y-C
# and you wish to remove X and Y:
git rebase Y C --onto B
# or (equivalent)
git rebase C~1 C --onto C~3
# both would result in: A-B-C'
# with this question's commit IDs:
git rebase 3371cec 2f05aba --onto e2b2a84
# or (equivalent)
git rebase 2f05aba~1 2f05aba --onto 2f05aba~3
Oftentimes I find myself just needing to remove a single commit from a feature branch's linear history, so assuming the commit ID to remove is X
and the commit ID after X
is K
(the commit you wish to "keep"):
git rebase K~1 K --onto K~2
Note that K~2
is one plus the number of commits in a row that you wish to remove. If you wish to remove 2 commits you would use K~3
, or 10 commits would be K~11
.
If you wish to preserve the changes in the commits you wish to remove, then you could use an interactive rebase (rebase -i
) and then change "pick" for those 2 commits to "squash" or "fixup".
If you wish to remove the commits as if they never happened, without keeping their changes either, then you can also use interactive rebase and in the instructions list, change "pick" for the two commits to "drop", or comment out the lines, or delete the lines, all of which will do the same thing.