1

Lets say I have a scenario:

I have 2 branches: master(default) and patch and both are live branches I created a topic branch from patch branch and wants to create a PR. When I create a PR, it tries to merge into master, as it is default.

Manually, I can change this, which has been answered in many post.

Merge pull request to a different branch than default, in Github

https://github.blog/2016-08-15-change-the-base-branch-of-a-pull-request/

https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-base-branch-of-a-pull-request

But I wanted to know is there any way I can automate this; can using hooks will be useful in this case. so whenever I create a PR, it will automatically will try to merge PR into its specific base branch instead of manually trying to overwrite it by edit the base branch.

Leo m
  • 11
  • 4
  • what have you tried so far? what isn't working? – anthony sottile Oct 11 '22 at 13:11
  • Since "base branch" is not a *Git* concept, whatever you do won't involve Git directly. Since each hook runs any arbitrary command(s) you like, you can do anything that's do-able in a hook, but it may be quite tricky. I doubt this is the right way to go, myself. – torek Oct 11 '22 at 17:22
  • @AnthonySottile so we have a git workflow in our system. I want to avoid the user to create a PR on wrong base branch by automating this edit on the fly. I wanted to know is it even possible – Leo m Oct 11 '22 at 19:00
  • @Leom quite frankly with arbitrary code execution anything is "possible" -- but you need to explore the solution space and see where you're stuck on things – anthony sottile Oct 11 '22 at 19:15

2 Answers2

1

I ran into a similar issue and found the following solution to be useful:

When you delete a branch, PRs that depend on it as a base will be updated to the base of the deleted branch.

GitHub has a feature to automatically delete the head branch upon merge - this has the side effect that "stacked" PRs (i.e. PRs that use that deleted branch as a base) get updated to the base of the merged PR automatically.

See:

Note: If you delete a head branch after its pull request has been merged, GitHub checks for any open pull requests in the same repository that specify the deleted branch as their base branch. GitHub automatically updates any such pull requests, changing their base branch to the merged pull request's base branch. For more information, see "About branches."

https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/incorporating-changes-from-a-pull-request/merging-a-pull-request

and to activate automatically deleting head branches upon merge:

https://docs.github.com/en/repositories/configuring-branches-and-merges-in-your-repository/configuring-pull-request-merges/managing-the-automatic-deletion-of-branches

dakami
  • 319
  • 2
  • 10
0

Another way to handle this is to use ezyang's ghstack. It takes a bit of getting used to, but it will handle stacking of branches and creates PRs with diffs that make sense.

nairbv
  • 4,045
  • 1
  • 24
  • 26