Created
December 30, 2023 20:27
-
-
Save aaronshaf/4330c31c8fdfbe3ac786293850986e0b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Check if an argument is provided | |
if [ "$#" -ne 1 ]; then | |
echo "Usage: $0 <number_of_branches>" | |
exit 1 | |
fi | |
# Number of branches to create | |
NUM_BRANCHES=$1 | |
# Current branch name | |
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD) | |
# Master branch name | |
MASTER_BRANCH="master" | |
# Get the list of changed files compared to master | |
CHANGED_FILES=$(git diff --name-only $MASTER_BRANCH...$CURRENT_BRANCH) | |
# Count the number of changed files | |
NUM_FILES=$(echo "$CHANGED_FILES" | wc -l) | |
# Calculate number of files per branch | |
FILES_PER_BRANCH=$(( (NUM_FILES + NUM_BRANCHES - 1) / NUM_BRANCHES )) | |
# Check out the master branch | |
git checkout $MASTER_BRANCH | |
# Create branches and cherry-pick changes | |
for i in $(seq 1 $NUM_BRANCHES); do | |
NEW_BRANCH="${CURRENT_BRANCH}-${i}" | |
git checkout -b $NEW_BRANCH $MASTER_BRANCH | |
# Calculate file range for this branch | |
START=$(( (i - 1) * FILES_PER_BRANCH + 1 )) | |
END=$(( i * FILES_PER_BRANCH )) | |
# Get the subset of files for this branch | |
FILES_TO_ADD=$(echo "$CHANGED_FILES" | sed -n "${START},${END}p") | |
# Add and commit the files | |
if [ ! -z "$FILES_TO_ADD" ]; then | |
git checkout $CURRENT_BRANCH -- $FILES_TO_ADD | |
git add $FILES_TO_ADD | |
git commit -m "Added changes for $NEW_BRANCH from $CURRENT_BRANCH" | |
git push origin HEAD:refs/for/master | |
fi | |
# Go back to master for next iteration | |
git checkout $MASTER_BRANCH | |
done | |
# Go back to the original branch | |
git checkout $CURRENT_BRANCH | |
echo "Created $NUM_BRANCHES branches off $MASTER_BRANCH with distributed changes from $CURRENT_BRANCH." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment