6

What options do you have if you want to "undo" a feature branch? Let's say you add a new feature supercool-feature which you finish (merge into development and delete the feature branch) and then it goes into a release. But then your users really dislike this supercool-feature. How can I undo/rewind/reverse this feature which has been already merged into development and a release?

I'm using SourceTree to do my versioning.

Hasturkun
  • 35,395
  • 6
  • 71
  • 104
Peter Warbo
  • 11,136
  • 14
  • 98
  • 193
  • 1
    Well you revert the merge... Obviously its a little more complex than that but the detail lies in how your organise your code and your tests etc. I've a feeling there's more to this question. – Alex Brown May 28 '13 at 14:22
  • Also this is not really a git-flow specific question. What git-flow has done for you is just create your feature branch from develop and then merge it back into develop. There is no magic stuff going on in git-flow, it's all just git. – Niklas Modess May 28 '13 at 14:25
  • @AlexBrown so how do I revert the merge? I'm not sure if you are familiar with how the git-flow process works. But when you finish a feature in git-flow it is merged into a "development" branch and then the feature branch is deleted. When you decide to make a release, you make a new branch from development and you make your release-specific changes and then merge this into "master". So how would I revert the `supercool-feature` that was merged into development? – Peter Warbo May 28 '13 at 16:34
  • Ah, I hadn't identified git-flow as a product - you are right I'm not familiar with it. However, in base git, the merge point of the feature branch should be a commit itself - simply find it and revert it. – Alex Brown May 28 '13 at 16:37
  • Use [`git revert`](http://stackoverflow.com/a/6217372/1615903). It fits the git-flow process best. – 1615903 May 29 '13 at 05:16
  • possible duplicate of [How to undo git flow feature finish?](http://stackoverflow.com/questions/13450039/how-to-undo-git-flow-feature-finish) – Hasturkun Jul 15 '13 at 16:05
  • @Hasturkun No, not a duplicate. In that other question, the merge commit was done locally but not pushed anywhere, and that's an important enough difference that the answers are completely different. –  Jul 15 '13 at 16:18
  • @hvd: Okay. I mostly marked since I removed Peter van der Does' comment about being duplicates. (since that didn't belong in the question) – Hasturkun Jul 16 '13 at 10:06
  • @Hasturkun Ah, thanks for the explanation, I hadn't seen that. Agreed, that shouldn't have been put in the question. –  Jul 16 '13 at 10:15

2 Answers2

16

Run this command

git revert -m 1 <sha1 of M>

Explanation

Your situation is about as follows:

A-B-----C---D-M     # master
   \         /
    X-Y---Z-        # supercool-feature

A, B, C and D were in your master branch, X, Y and Z in your feature branch. When you did git merge supercool-feature Git created a "merge commit" M for you. This commit encloses all commits from your feature branch as well as possible fixes to fix merge conflicts. So if you revert that one commit, every change from your featurebranch will be gone again.

Andrew
  • 18,680
  • 13
  • 103
  • 118
Nils Werner
  • 34,832
  • 7
  • 76
  • 98
  • devils advocate: how does it know to remove everything from supercool feature XZY, and not everything from master (CD)? the merge is symmetric... – mnagel Jul 15 '13 at 18:23
  • Merges are not symmetric. `git merge` looks at the changeset from the branch to be merged (`git diff B..Z`) and squashes it into one commit. – Nils Werner Jul 15 '13 at 19:09
  • after reverting the merge commit, what if we need that feature again in future? The feature branch has already been deleted and the merge commit reverted, wouldn't all the code related to feature be lost ? – gaurav5430 Jan 21 '20 at 17:55
  • @gaurav5430 You could always create a branch at the tip of that "reverted" feature branch. E.g. `git branch feature/my-reverted-feature {sha}`. Since it's now "referenced", you'll have access to the branch's history and it won't be cleaned up by Git. – fourpastmidnight Mar 09 '20 at 19:17
0

Actually the command is

git revert -m 1 <sha1 of M>

which remove M moving back on branch containing X,Y,Z which is not the mainline 1.