GitHubでMerge済みのPull requestにCommitを追加して再度Mergeできるのか確認してみた

2020.07.18

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

こんにちは、CX事業本部の若槻です。

GitHubでの開発で、機能BranchからメインBranchにPull requestをMergeした後に、同Branchに含めるべき変更があることに後になって気付くことがあります。

今回は、そのような場合にGitHubでMerge済みのPull requestにCommitを追加して再度Mergeできるのか?を確認してみました。

確認してみた

メインBranchmasterから機能Branchfeature-modify-readmeにMerge済みのPull requestがあります。 image.png

しかし、同Branchに含め損ねた変更があったことに気づいたので、その含めるべきだった変更のCommitを機能Branchに追加して、リモートにPushしてみます。

$ git commit -a -m "fix"
[feature-modify-readme 9393aed] fix
 1 file changed, 2 insertions(+), 1 deletion(-)
$ git push origin feature-modify-readme
Counting objects: 3, done.
Writing objects: 100% (3/3), 287 bytes | 287.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/username/hoge-system.git
   e74c20d..9393aed  feature-modify-readme -> feature-modify-readme

同BranchのPull requestを確認しても追加したCommitは取り込まれている様子はありません。 image.png

結論として、Merge済みでCloseされたPull RequestのBranchに変更を追加しても、Pull Requestに追加されたり、再度Merge可能になったりはしないようです…。

どうすればよいか

横着せずに新しいBranchとPull requestを改めて作成してからMergeしましょう。

追加でMergeしたかったCommitが含まれる元の機能Branchから新しい機能Branchを作成してPushします。

$ git branch
* feature-modify-readme
  master
$ git checkout -b fix-feature-modify-readme
Switched to a new branch 'fix-feature-modify-readme'
$ git push origin fix-feature-modify-readme
Total 0 (delta 0), reused 0 (delta 0)
remote: 
remote: Create a pull request for 'fix-feature-modify-readme' on GitHub by visiting:
remote:      https://github.com/username/hoge-system/pull/new/fix-feature-modify-readme
remote: 
To https://github.com/username/hoge-system.git
 * [new branch]      fix-feature-modify-readme -> fix-feature-modify-readme

すると当然ながら新しいBranchでPull requestが作れるようになるので、 image.png

Pull requestを作って、masterBranchへのMergeを実施すると、 image.png

masterBranchにMergeできました。 image.png

おわりに

GitHubでMerge済みのPull requestにCommitを追加して再度Mergeできるのかを確認してみました。

オペレーション自体は何てことないですが、TeamのメンバーにPull requestのレビューを再度依頼することの方が忍びなくて心理的ハードルが高いと思うので、こうならないようにPull Requestのレビューを出す前やMerge前にセルフチェックをしっかりやりましょう。

以上