Skip to content

Instantly share code, notes, and snippets.

@suya55
Forked from asit-dhal/git-clean-local-branches
Last active February 13, 2019 04:46
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suya55/af228e576aead0c47c12c2baf0427951 to your computer and use it in GitHub Desktop.
Save suya55/af228e576aead0c47c12c2baf0427951 to your computer and use it in GitHub Desktop.
오래된 브랜치를 지우기 위한 스크립트. master, develop,HEAD은 제외됨. 자세한건 -h 로 확인.
#!/bin/bash
#
# Copyright(c) 2018 Asit Dhal.
# Distributed under the MIT License (http://opensource.org/licenses/MIT)
#
LIGHT_BLUE='\033[1;34m'
COLOR_YELLOW='\e[1;33m'
COLOR_CYAN='\e[0;36m'
COLOR_PURPLE='\e[0;35m'
RED='\033[0;31m'
NC='\033[0m' # No Color
INTERACTIVE=""
FORCE_DELETE=""
MASTER="master"
DEVELOP="develop"
HEAD="HEAD"
DELETE_ORIGIN=""
UNTIL_DATE=""
UNTIL=""
TODAY=$(date +'%Y-%m-%d')
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-f)
FORCE_DELETE="y"
shift
;;
-d)
UNTIL_DATE="y"
shift
UNTIL=$1
shift
;;
-o)
DELETE_ORIGIN="y"
shift
;;
-i)
INTERACTIVE="i"
shift
;;
-h)
echo "usage: $(basename $0) [-ifo] [-d date]"
echo " $(basename $0)"
echo " $(basename $0) -i -f -o -d 2018-12-31"
echo " -i Intecative deletion of local branches"
echo " -f Force delete the branch(es)"
echo " -o Delete remote origin branch(es)"
echo " -d YYYY-MM-DD Delete until date : 2018-12-31"
echo " List local branches with creation and last update date"
exit
;;
*)
shift
esac
done
current_branch=$(git branch | grep "*")
current_branch=${current_branch/* /}
if [ "${current_branch}" != "${MASTER}" ]; then
printf "Please change the current branch to ${LIGHT_BLUE}${MASTER}${NC}\n"
exit
fi
if ! git diff-files --quiet; then
printf "you have unstaged changes. $(basename $0) needs a clean working index\n"
git diff-files --name-status
printf "Please commit or stash them.\n"
exit
fi
if ! git diff-index --cached --quiet HEAD; then
printf "your index contains uncommitted changes. $(basename $0) needs a clean working index\n"
git diff-index --cached --name-status HEAD
printf "Please commit or stash them.\n"
exit
fi
ALL_DELETE=""
for brnch in $(git branch -ra | sed /\*/d | sed -e "s/->//g"); do
if [[ $brnch == *"$DEVELOP"* || $brnch == *"$MASTER"* || $brnch == *"$HEAD"* ]]; then
continue
fi
branch_name=$(echo "$brnch" | sed -e "s/remotes\/origin\///g")
created_commit_id=$(git merge-base ${brnch} ${MASTER})
created_date=$(git log --pretty=format:"%ad" --date=short -n 1 ${created_commit_id})
created_ago=$(git log --pretty=format:"%cr" --date=short -n 1 ${created_commit_id})
last_updated_date=$(git log --pretty=format:"%ad" --date=short -n 1 ${brnch})
updated_before=$(git log --pretty=format:"%cr" --date=short -n 1 ${brnch})
commit_message_subject=$(git log --pretty=format:'%s' -n 1 ${brnch})
if [[ "${UNTIL}" < "${last_updated_date}" ]]; then
printf "${COLOR_CYAN}SKIP Branch : ${branch_name} ${NC}\n\n"
continue
fi
printf "Branch name : ${COLOR_YELLOW}${branch_name} ${NC}\n"
printf "Created on : ${created_date}${RED}(${created_ago})${NC}\n"
printf "Last updated on : ${last_updated_date}${RED}(${updated_before}${NC})\n"
printf "Last commit message : ${commit_message_subject}\n"
if [ "${INTERACTIVE}" == "i" ]; then
if [ "${ALL_DELETE}" == "y" ]; then
ip="y"
else
printf "${LIGHT_BLUE}Delete the branch, followed by [y/n/a]?${NC} "
read ip
if [ "$ip" == "a" ]; then
ALL_DELETE="y"
ip="y"
fi
fi
if [ "$ip" == "y" ]; then
if [ "${FORCE_DELETE}" == "y" ]; then
printf "> git branch -D ${branch_name} >> ${COLOR_PURPLE}"
git branch -D ${branch_name}
printf "${NC}"
else
printf "> git branch -d ${branch_name} >> ${COLOR_PURPLE}"
git branch -d ${branch_name}
printf "${NC}"
if [ $? -ne 0 ]; then
printf "${LIGHT_BLUE}Run with -fi command to delete ${branch_name}${NC}\n"
fi
fi
if [ "${DELETE_ORIGIN}" == "y" ]; then
printf "> git push origin :${branch_name} >> ${COLOR_PURPLE}"
git push origin :${branch_name}
printf "${NC}"
fi
fi
printf "\n"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment