You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am going to leave this here for a week in case anyone finds it useful.
I have created a simple script for myself and I am sharing it here in case anyone wants to use it. The script was inspired and built upon @ekohl's cherry-picking script.
What it does
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>
...
Prerequisites
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
Issues
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.
Installation
Create a file in your ~/bin/ directory a paste the contents of the script to it. You can name it whatever you'd like.
Usage
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.
Example use
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}
Disclaimer
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!
#!/bin/bash
i=1
BOTTOM=master
while [ $i-le$1 ]
do
BOTTOM+=^
i=$(($i+1))done
COMMITS="${BOTTOM}..master";shift
BRANCHES=$*
REMOTE=upstream
if [[ -z$COMMITS ]] ;thenecho"Usage: $0 NCOMMITS BRANCHES"echo" NCOMMITS number of commits"echo" BRANCHES branches to cherry-pick the commits to"exit 1
fi
git checkout master && git pull
forbranchin$BRANCHES;do
git checkout $branch
git pull
git cherry-pick -x $COMMITSdoneecho The following has been copied to you clipboard:
echo"git push $REMOTE$BRANCHES 2>&1 | tail -n $# | awk '{if (NR == 1) printf(\"Merged and cherry-picked:\n\"); print}' | xclip -sel clip"echo"git push $REMOTE$BRANCHES 2>&1 | tail -n $# | awk '{if (NR == 1) printf(\"Merged and cherry-picked:\n\"); print}' | xclip -sel clip"| xclip -sel clip
The text was updated successfully, but these errors were encountered:
Interesting approach for the number of commits. I always like to review them before I pick. Consider adding git log --oneline $COMMITS.
I also use set -e so it fails hard. Right you when there is a conflict you may end up with partial cherry picks. Usually when you fail to pick to 3.x, 3.(x-1) also fails.
And for Wayland users there's wl-copy instead of xclip.
I am going to leave this here for a week in case anyone finds it useful.
I have created a simple script for myself and I am sharing it here in case anyone wants to use it. The script was inspired and built upon @ekohl's cherry-picking script.
What it does
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:
Prerequisites
Issues
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.
Installation
Create a file in your
~/bin/
directory a paste the contents of the script to it. You can name it whatever you'd like.Usage
Example use
Disclaimer
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!
The text was updated successfully, but these errors were encountered: