Change a commit message that is already merged by pull request on a protected branch on GitHub

Strategy

We have several ways to change the commit message.

Double Reverts

Revert the PR

GitHub Allow you to revert a PR. It is just create additional commit for revert all the change. Since Git is event sourcing pattern, the old commit is still remains, however, we want to add the proper commit message against the change. Go to the PR. You will find revert button.

revert
Revert PR

Fetch the pull request and change the message

Once You’ve got the pull request, You can fetch the pull request branch. It will create a new branch revertrevert with the change of the pull request.

git fetch origin pull/<YOUR PullRequest Number>/head:revertrevert
git switch revertrevert
$ git log
commit 0456ee900a4cb0dfd2ee2507b89ec84e5f52ad44 (HEAD -> revertrevert)
Author: Tsuyoshi Ushio <ushio@simplearchitect.com>
Date: Tue May 5 23:28:42 2020 -0700
Revert "Revert "Can't change directory""commit 0424d09025e2764a0e713fc36e991c0eac4cf074
Merge: cdc9bab ba74ece
Author: Tsuyoshi Ushio <ushio@simplearchitect.com>
Date: Tue May 5 23:28:33 2020 -0700
Merge pull request #2 from TsuyoshiUshio/revert-1-TsuyoshiUshio-patch-1Revert "Can't change directory"
git commit --amend
$ git log
commit 87c77062b5b450818d90e09462ecdc48759f9a8f (HEAD -> revertrevert)
Author: Tsuyoshi Ushio <ushio@simplearchitect.com>
Date: Tue May 5 23:28:42 2020 -0700
This is the proper commit message.commit 0424d09025e2764a0e713fc36e991c0eac4cf074
Merge: cdc9bab ba74ece
Author: Tsuyoshi Ushio <ushio@simplearchitect.com>
Date: Tue May 5 23:28:33 2020 -0700
Merge pull request #2 from TsuyoshiUshio/revert-1-TsuyoshiUshio-patch-1Revert "Can't change directory"

Create a new PR

However, no worries, you can push your branch to your folked repo or the target repository. I have an access right to that repository.

git push --set-upstream origin revertrevert
New PR from the revertrevert branch
Commit message changed!

Resources

If you want to configure your repo to avoid direct commit to the master branch, you can configure the protected branch with Require pull request reviews before merging and include administrators That protect master branch even if you are an administrator.

Protected branch

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Tsuyoshi Ushio

Tsuyoshi Ushio

Senior Software Engineer — Microsoft