Let's say I have these commits in chronological order:
Now, I want to get rid of b
but keep c, so that I have:
How do I do this?
(there are no conflicts between b
and c
)
In case you already pushed the changes to a remote, you could use:
$ git revert <hash of commit b>
that creates a new commit d
that removes the changes of commit b
Assuming you haven't already pushed to a remote repository, you can do an interactive rebase. See here:
If you only need the one commit from the HEAD, you can use cherry-pick on a different branch to bring just that commit over:
$ git checkout -b working
$ git reset --hard <hash of the commit `a`>
$ git cherry-pick <hash of the commit `c`>
The hard reset changes the working copy back to the state as of that commit, and then the cherry-pick applies the changes introduced in commit c
directly on top of your working copy.
The help for git rebase talks about this exact case! Check it out:
A range of commits could also be removed with rebase. If we have the
following situation:
E---F---G---H---I---J topicA
then the command
git rebase --onto topicA~5 topicA~3 topicA
would result in the removal of commits F and G:
E---H'---I'---J' topicA
This is useful if F and G were flawed in some way, or should not be
part of topicA. Note that the argument to --onto and the <upstream>
parameter can be any valid commit-ish.