26

We have protected our develop branch on GitHub so that nobody downstream can push their commit directly. The commits need to go through specific feature branch and get merged through a pull request.

There came a scenario where a feature branch is merged into the develop branch (after proper review and changes) and we are required to revert it later (maybe due to changes in requirements). If I try to revert the merge commit downstream, it will not allow me to push, since the branch is protected. I remember GitHub providing revert button when we merge the branch. But somehow I am not able to see (or find) the button now. We needed to revert the commit on priority so we removed the protection from the develop branch for the time being and pushed the revert commit (ugliest hack).

Are there any other better alternative for reverting a commit from protected branch? Maybe I am missing or misunderstood some GitHub features.

One more scenario is, what if I have deleted the branch from GitHub after I have merged, how would I revert it then?

nak
  • 846
  • 2
  • 10
  • 26
  • 1
    Did you try using [`git revert`](https://git-scm.com/docs/git-revert) and making a new pull request from the downstream branch? `git revert` should create a new commit that is ahead of the upstream HEAD – rink.attendant.6 Mar 02 '17 at 07:13
  • @rink.attendant.6: I didn't try it but it definitely looks one of the elegant solutions originating from downstream. Looks simple also and there I was overthinking this stuff!. Are there any solutions on GitHub? which I can use because it's preferable for people to click a button than remember and write commands. – nak Mar 02 '17 at 07:32

2 Answers2

12

Reverting on GitHub

You don't need to restore (undelete) branches on GitHub to revert merge commits resulting from pull requests. For example:

Revertable pull request

Non-revertible pull request

Sometimes the revert button doesn't appear. From GitHub Help on reverting a pull request:

Note: You may need to use Git to manually revert the individual commits if:

  • Reverting the pull request causes merge conflicts
  • The original pull request was not originally merged on GitHub (for example, using a fast-forward merge on the command line)

It took me a while to find an example, but if the head branch wasn't merged into the base branch using the big green button on GitHub then it can't be reverted on GitHub:

Non-revertable pull request

git revert

Locally on the command line, you can use the git revert command to revert changes.

This will work on both your protected branch and a downstream branch. git revert creates a new commit ahead of the current HEAD so you don't need to force push, and if from a downstream branch, you can manually create a pull request for the reverted changes.

Reverting a merge commit is slightly more complicated than reverting a single-parent commit, so I'd suggest taking a look at this question for more information, as it's something I've never done before.

If people aren't comfortable using the command line, I think SourceTree has an item on the context menu to revert a commit but I don't know how it handles merge commits. There might be similar options in other GUI applications.

Hope this helps!

Community
  • 1
  • 1
rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
  • Even If I revert a commit in downstream tracking branch and try to push to protected branch it will still not allow since protected branch does not allow any new commit pushes from downstream. A way would be to create a new branch from the downstream tracking branch, revert the changes, push the new branch and merge it to protected branch – nak Mar 02 '17 at 09:19
  • Great answer - linked off to the answer for reverting a merge commit - the version I needed. – RudyOnRails Jan 29 '19 at 18:37
1

The most easiest option is to perform the following

git revert -m 1 "last_commit_id"
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Vinod Kumar
  • 562
  • 5
  • 12