Skip to content

Instantly share code, notes, and snippets.

@augustogiles
Last active April 3, 2024 17:21
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save augustogiles/528176e0eac6b066195ff25e10f52849 to your computer and use it in GitHub Desktop.
Save augustogiles/528176e0eac6b066195ff25e10f52849 to your computer and use it in GitHub Desktop.
Based in a branch, all the branches that already merged in it is passive to be archived.
#!/usr/bin/env bash
current_branch=$(git rev-parse --abbrev-ref HEAD)
target_branch=$1
if [ -z "$target_branch" ]
then
target_branch=$current_branch
fi
response="DECIDE"
while ! [[ $response =~ ^([yY])$ ]];
do
read -p "Do you want to run this script based in branch $target_branch? [Y|N] " response
echo
if [[ $response =~ ^([yY])$ ]]
then
echo "Ok, proceeding"
elif [[ $response =~ ^([nN])$ ]]
then
echo "No, terminating"
exit 0
fi
done
branches=$(git branch -r --merged | grep -v HEAD | grep -v master | grep -v develop | grep -v "release_*")
branch_amount=$(echo "$branches" | wc -l)
echo "$branch_amount branches to archive"
while read branch; do
BRANCH=${branch##*/}
response_branch="DECIDE"
while ! [[ $response_branch =~ ^([yY])$|^([nN])$ ]];
do
read -p "Do you want to archive branch $BRANCH ? [Y|N] " response_branch </dev/tty
if [[ $response_branch =~ ^([yY])$ ]]
then
echo "Ok, proceeding"
echo "======= Checking out to $BRANCH ======="
git checkout -b $BRANCH
echo "======= pulling branch ======="
git pull origin $BRANCH
echo "======= Tagging branch to archive ======="
git tag archive/$BRANCH $BRANCH
echo "======= Removing branch $BRANCH ======="
git checkout $target_branch
git branch -d $BRANCH
git push origin :$BRANCH
elif [[ $response_branch =~ ^([nN])$ ]]
then
echo "Skipping branch $BRANCH ..."
fi
done
done <<< "$branches"
echo "======= Pushing tags ======="
git push --tags
echo "END OF SCRIPT"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment