0

Let's say we have three different branches:

  1. Main Branch - Here the developer pushes new code changes for new features
  2. Release Branch - This is the Production Branch
  3. Demo Branch - This branch is used to demonstrate new features to the customers.

So now the problem is that whenever I need to provide a hotfix to an escalation on the release branch, I have to create three different PR manually each for the Main, Release, and Demo branches for the same changes which is quite a manual task and a bit frustrating.

Currently, I commit the changes to the Release branch and cherry-pick the commits to the other branches, and create separate PRs for all these three branches.

Is there any way to create multiple PRs for the same change for multiple branches in one go?

Appreciate your help or any suggestion Thanks!

Arpit Jain
  • 1,599
  • 9
  • 23
  • The thing to understand clearly is that there is _no such thing as a pull request_ as far as Git is concerned. The creation, the very existence, of pull requests is entirely a feature of the _particular hosting site_ you're talking to (GitHub, or maybe BitBucket, or whatever). There is no reason why that site _couldn't_ implement the shortcut you're describing by way of its web interface; but that is between you and that site. If you have a complaint or suggestion about how the interface of your hosting site works, talk to the maintainers of that site. – matt Jan 08 '23 at 19:30

3 Answers3

1

There is no feature like this.

A pull request is from a branch to a branch (a pr is not for a commit) and a pr from hotfix1 branch (based on release branch) to release if 'repeated' onto main and/or demo will yield undesirable results in most cases. 'Repeating' a PR into multiple branches is not the same as cherry-picking a hotfix commit (the cherry-pick PR creates a 'temp' branch from the target branch).


How this could work

Let's say that the feature works for single commit PRs only for simplicity. When creating the PR from hotfix1toreleasethere would be an option to select extra target branches for an automatically created cherry-pick tomain, demo`, etc. I suppose this would be possible, but GitHub doesn't have it at the moment.

tymtam
  • 31,798
  • 8
  • 86
  • 126
1

You can use the Github CLI and a bash script to create PRs to multiple branches. For example:

for branch in main release
do
   gh pr create -B $branch -t "My PR title" -b "My PR description"
done
D Malan
  • 10,272
  • 3
  • 25
  • 50
1

Thanks to @D Malan for the suggestion, I created the below shell script. Hope it can help others as well.

#!/bin/sh

declare ISSUE_ID COMMIT_MESSAGE PR_TITLE PR_DESSCRIPTION PR_MODE;

receive_user_input() {
  echo "These are the List of Modified Files I could find:-";
  git ls-files -m;
      
  read -p "Provide a valid ID for this fix: " ISSUE_ID;

  read -p "Name the other remote branches (space separated) you want to create PR for: " REMOTE_BRANCHES;
 
  read -p "Provide a proper commit message: " COMMIT_MESSAGE;
  
  read -p "Provide a title for your PR: " PR_TITLE;
  
  read -p "Provide a description for your PR: " PR_DESSCRIPTION;

  read -p "Do you want to create PR in draft Mode (Y/N)? : " PR_MODE;
  
};

create_pull_request() {

  # use to exit when the command exits with a non-zero status.
  set -e;
  
  # Commit changes to the hotfix branch and create a PR.
  {
    git checkout -b hotfix/$ISSUE_ID;
    git add $(git ls-files -m);
    git commit -m "$COMMIT_MESSAGE";
    COMMIT_ID=$(git rev-parse HEAD);
    git push --set-upstream origin hotfix/$ISSUE_ID;
    gh pr create --base release --head hotfix/$ISSUE_ID --title "$PR_TITLE" --body "$PR_DESSCRIPTION" --assignee "@me" --draft; 
    
    # Loop over the other remote branches and cherry-pick the commit and create PR.
    for branch in $REMOTE_BRANCHES ; do
      git fetch origin;
      git checkout $branch;
      git pull origin $branch;
      git checkout -b fix/$branch/$ISSUE_ID;
      git cherry-pick $COMMIT_ID;
      git push origin fix/$branch/$ISSUE_ID;
      gh pr create --base "$branch" --head "fix/$branch/$ISSUE_ID" --title "$PR_TITLE" --body "$PR_DESSCRIPTION" --assignee "@me" --draft; 
    done
  } > logs.txt 2>&1;
  # 1.command > logs.txt: redirects the output of command(stdout) to logs.txt file.
  # 2.2>&1: redirects stderr to stdout, so errors (if any) also go to the logs.txt file.
};

receive_user_input;
create_pull_request;
Arpit Jain
  • 1,599
  • 9
  • 23