Skip to content

Instantly share code, notes, and snippets.

@namnv609
Last active June 3, 2022 08:23
Show Gist options
  • Save namnv609/7b130aaca35573af4f05dd7b298e3219 to your computer and use it in GitHub Desktop.
Save namnv609/7b130aaca35573af4f05dd7b298e3219 to your computer and use it in GitHub Desktop.
[Bash] Git bulk rebase

Git bulk rebase

Use case

When you have multiple branches need to rebase with same a branch. You need to switch to each branch and rebase it. This script help you to automate switch and rebase with each branch with main branch (auto get or you can passing it into first argument)

Usage

./git-bulk-rebase.sh <main branch>
#!/usr/bin/env bash
main_branch="$1"
RED="\033[1;31m"
GREEN="\033[1;32m"
NC="\033[0m"
if [ -z "$main_branch" ];then
main_branch=$(git branch | cut -c 3- | grep -E "^milestone_")
fi
function bulk_rebase() {
for branch in $(git branch | cut -c 3-); do
if [[ ! "$branch" =~ ^(milestone_|develop) ]]; then
echo -en "Do you want to rebase branch [${GREEN}${branch}${NC}] with branch [${RED}${main_branch}${NC}] (y/n/q)?: "
read answer
if [[ "$answer" =~ q|Q ]]; then
break
elif [[ "$answer" =~ y|Y ]]; then
(git checkout "$branch" && git rebase "$main_branch")
if [ $? -ne "0" ]; then
echo "Error when rebase [${GREEN}${branch}${NC}] with [${RED}${main_branch}${NC}]. Please check it!"
break
fi
fi
fi
done
}
bulk_rebase
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment