Change a commit message that is already merged by pull request on a protected branch on GitHub
I made a mistake a commit message. That has multiple lines. However, the approver already merged it and after merged realized that it is not proper commit message, how can we solve this problem?
Strategy
We have several ways to change the commit message.
For example, you can remove the protected branch for a while then git commit --amend
against the master branch. Or you might create a PR, that rebase the old commit. I’ve never tried, however it might works. However, these strategy is not a good idea. If your repo has contributors, the contributor might checkout or folk your master branch, then you amend/rebase it, it affect them. Let’s think about the safer way.
It might be a double reverts
strategy. My colleague Ahmed come up with the idea.
How it works.
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.

Click revert, then you will see the new PR.

Create a new PR that will revert the change then merge it.
After the PR is merged, find the revert
button. That means, it revert the revert change. That means it includes all the change of the first pull request. Click revert
and create a new 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
The current commit message is like this.
$ git log
commit 0456ee900a4cb0dfd2ee2507b89ec84e5f52ad44 (HEAD -> revertrevert)
Author: Tsuyoshi Ushio <ushio@simplearchitect.com>
Date: Tue May 5 23:28:42 2020 -0700Revert "Revert "Can't change directory""commit 0424d09025e2764a0e713fc36e991c0eac4cf074
Merge: cdc9bab ba74ece
Author: Tsuyoshi Ushio <ushio@simplearchitect.com>
Date: Tue May 5 23:28:33 2020 -0700Merge pull request #2 from TsuyoshiUshio/revert-1-TsuyoshiUshio-patch-1Revert "Can't change directory"
Change the commit message. You can use git commit --amend
for the latest commit change. It must be the latest commit. Amend it.
git commit --amend
You will see the screen that enable us to change it. You will see the git commit --amend
will change the commit id. Now, we have a proper message.
$ git log
commit 87c77062b5b450818d90e09462ecdc48759f9a8f (HEAD -> revertrevert)
Author: Tsuyoshi Ushio <ushio@simplearchitect.com>
Date: Tue May 5 23:28:42 2020 -0700This is the proper commit message.commit 0424d09025e2764a0e713fc36e991c0eac4cf074
Merge: cdc9bab ba74ece
Author: Tsuyoshi Ushio <ushio@simplearchitect.com>
Date: Tue May 5 23:28:33 2020 -0700Merge pull request #2 from TsuyoshiUshio/revert-1-TsuyoshiUshio-patch-1Revert "Can't change directory"
However, you can’t push the pull request.
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
Then you can create a PR from this branch. You will have one more PR already, let’s close it (the Rever “Revert “… one)

Use the last PR instead. Then it works.

Merge it!

It works!
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.
