Skip to content

Instantly share code, notes, and snippets.

@alan707
Last active April 6, 2023 21:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alan707/d88d402199000a14b24804d64d1bf128 to your computer and use it in GitHub Desktop.
Save alan707/d88d402199000a14b24804d64d1bf128 to your computer and use it in GitHub Desktop.
Rebase branch names on top of develop
#!/bin/bash
usage() {
echo "Usage: $0 [branch1 branch2 branch3 ...] or $0 [branches.txt]"
echo "Arguments:"
echo " branch1 branch2 branch3 ... : A list of branch names separated by spaces"
echo " branches.txt : A text file containing branch names, one per line"
echo "Rebases the specified branches on top of the 'develop' branch and asks for confirmation to force push them to the origin."
}
rebase_branches() {
for branch in "${branches[@]}"; do
git checkout "$branch"
git rebase develop
echo "Branch $branch has been rebased on top of develop"
done
}
push_branches() {
force_push_all=$1
for branch in "${branches[@]}"; do
if [ "$force_push_all" == "true" ]; then
git push -f origin "$branch"
echo "Branch $branch has been force pushed to origin"
else
read -p "Do you want to force push branch $branch? (y/n) " choice
case "$choice" in
y|Y) git push -f origin "$branch"
echo "Branch $branch has been force pushed to origin";;
n|N) echo "Branch $branch was not pushed to origin";;
*) echo "Invalid option";;
esac
fi
done
}
if [ $# -eq 0 ]; then
usage
exit 1
fi
if [ -f "$1" ]; then
branches=($(cat "$1"))
if [ ${#branches[@]} -eq 0 ]; then
echo "The file is empty or contains invalid branch names"
exit 1
fi
else
branches=("$@")
fi
git checkout develop
git pull
rebase_branches
read -p "Do you want to force push all rebased branches to origin? (y/n) " choice
case "$choice" in
y|Y) push_branches "true";;
n|N) push_branches "false";;
*) echo "Invalid option";;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment