Reputation: 51
I have some commits in my branch say commit A commit B commit C commit D commit E
I have to delete commit D and keep commit A, B, C as it is. These commits are already pushed to remote branch.
I have tried sol1 but it was not allowing me to force push my changes to remote branch.
I do not want to do git reset for the commits A, B and C and want to keep them as it is.
How can that be done?
Upvotes: 0
Views: 130
Reputation: 45819
There are two ways to remove D
's changes while keeping A
, B
, C
, and E
's changes.
One is to revert D
.
git revert HEAD^
git push
This will create a new commit, ~D
, after E
. Because it does not change history it does not require force-pushing, which addresses what you say stopped you in your previous solution attempt. It does mean that the history will show the changes from D
and their reversal; some people don't like that, but you may have to ask yourself whether that's a vanity you can afford.
You can learn more about revert here: https://git-scm.com/docs/git-revert
The other option is to rewrite history. It sounds like you already tried this and couldn't get the remote to accept the force push. There are good reasons people configure remotes to reject force pushes, and while it does depend on your team and project, you should understand the costs associated with force pushing.
The thing to understand is, it can seem like there are lots of solutions to physically remove D
- rebase, reset, cherry pick, maybe even filter-branch... But they all boil down to the same thing. If, at the end of processing, you want D
(which was previously in the history of the branch) to no longer be in the history of the branch, then you are trying to rewrite history and need to be able to force push. So if the force-push is what failed - i.e. if you've reached a state where locally you have
A -- B -- C -- E'
and you just can't push that to the remote - then there's no point looking for other history-rewriting techniques; they'll all conclude with the step "force push to the remote".
So in that case, the only remaining option is git revert
.
Upvotes: 2