1

Is it possible to revert a merge commit on master from a feature branch that have already been pushed to the remote?

E.g. create a feature branch from the merge commit in master, revert the commit and merge in the feature branch?

Or do you need to do the revert on master and push from master?

I am working in a setting where changes to master is only allowed through Pull Request branches so investigating if we can somehow use the git revert merge commit operations as described here:

Undo a Git merge that hasn't been pushed yet

u123
  • 15,603
  • 58
  • 186
  • 303

2 Answers2

3

Create a branch where you revert the commit, then merge it back with master.

Something like this :

git checkout master
git pull
git checkout -b feature/revert_dev
git revert [hash of the commit that you want to revert]
git push -u origin (or whatever you are naming your remote) feature/revert_dev

then merge feature/revert_dev with master

Hamza El Aoutar
  • 5,292
  • 2
  • 17
  • 23
  • In step 4 I get "error: commit 4bff9f5c1e8aef471b2e6471ae2e37d55c62d392 is a merge but no -m option was given." Notice in my post case i limited to when the commit is a merge commit. – u123 May 07 '18 at 13:49
  • 1
    You can use the hash of the commit that you had on your branch before you merged it, or you can use `git revert` with -m option : `git revert -m 1 [hash of the merge commit]` – Hamza El Aoutar May 07 '18 at 14:00
1

If you're going to revert a merge, you should be sure you understand the implication, because they aren't obvious. IF you understand the implications and still want to revert the merge, then the protected master branch is not an impediment.

The reason the protected branch doesn't matter is, a revert is just a commit whose changes (relative to its parent) happen to undo changes from one or more other commits (relative to their parents). It can be done on a branch and then merged using a PR, or whatever other process is required for introducing a commit that changes master.

The revert itself will require special arguments, to tell gitwhich parent the merge is to revert to. And more importantly, once the revert is merged into master, it will be difficult to re-merge the branch whose merge you've reverted (because git will see it as "already merged"). Your options at that point will be to "revert the revert", or to create new commits that have the same effect as the branch (e.g. by doing a rebase -f on it).

Mark Adelsberger
  • 42,148
  • 4
  • 35
  • 52