3

Imagine this scenario:

master:            M
release-candidate:  \     A--B'--C'
feature-A:           \-A-/  /   /
feature-B:            \-B--/   /
feature-C:             \--C---/

The release-candidate was created by using the following commands:

git co master
git co -b release-candidate
git merge feature-A (FAST FORWARD)
git merge feature-B (REGULAR MERGE)
git merge feature-C (REGULAR MERGE)

Let's say during our pre-release testing we find a problem with feature A. How do we undo the changes introduced by the fast-forward merge of this feature branch into the release-candidate branch?

We could potentially reset release-candidate back to its master state and start merging all the valid features again but this would mean repeating a lot of merge conflict resolution and introduces the possibility for error.

Note also that I've only shown a single commit on each of the feature branches but a solution should work in the case where there is more than one commit to undo.

1 Answer 1

4

As you do not want to rewrite published history, git revert is your friend.

In this case:

git revert M..A -n
git commit -m "revert feature-A"

if you want the revert in one commit, or

git revert M..A

if you want to have one revert commit per original commit in reversed order.

2
  • Does that work reliably even when commits A and B/C have conflicts?
    – Mifeet
    Commented May 9, 2016 at 21:21
  • Yes, it should. If there are conflicts, revert will stop, ask you to resolve the conflicts and then you can do git revert --continue. If this does not work good with the -n option, use the second variant and after everything is reverted use git rebase -i to squash the commits if wanted.
    – Vampire
    Commented May 10, 2016 at 6:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.