The script cherry-picks a required number of the latest commits from the master branch to required branches. Additionally, the script copies a command to your clipboard, which when used, will push the branches with cherry-picked commits to upstream repository and will store a formatted message in your clipboard, which you then just have to paste in a comment when merging and cherry-picking a PR.
The formatted message has the following format:
Merged and cherry-picked:
<BRANCH1_OLD_LATEST_COMMIT>..<BRANCH1_NEW_LATEST_COMMIT> -> <BRANCH1_NAME>
<BRANCH2_OLD_LATEST_COMMIT>..<BRANCH2_NEW_LATEST_COMMIT> -> <BRANCH2_NAME>
...
- The script uses the xclip utility to copy first the push command and then the formatted message to your clipboard. This utility is normally not included in the base Linux installation so you'll need to install it if you haven't done so yet.
- The script uses the bash shell
The script stops if the was a merge-conflict when cherry-picking, but will still paste the push command to your clipboard. That being said, the push command does not do anything to a specific branch if new changes were not committed to it.
Create a file in your ~/bin/
directory a paste the contents of the script to it. You can name it whatever you'd like.
- Run the script
$ SCRIPT NCOMMITS BRANCHES
- SCRIPT is how you name the script. As I am only pasting a code here, not the file, you can name it whatever you want.
- NCOMMITS - number of the latest commits you want to cherry-pick
- BRANCHES - branches you want to cherry-pick to
- If the cherry-picking is executed successfully (no merge conflicts), paste the command from your clipboard to the terminal (CTRL + SHIFT + V) and run the command.
- Paste the formatted message from your clipboard to the comment of the merged PR.
- Cherry-pick two latest commits from master to 3.5 and 3.4:
foreman-cherry-pick 2 3.5 3.4
- Cherry-pick the latest commit from master to branches 3.5 through 3.1:
foreman-cherry-pick 1 3.{5..1}
Script is in no way optimized nor written in a pretty way, but it works for me and maybe it can help you too. You can easily optimize it to suit your needs. Enjoy!