/
bash_code
/
gitdel
Обзор
Документация
Войти
/
bash_code
/
gitdel
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
master
gitdel_en
98 строк
3 KB
codesshaman
initial commit
08 апр 2026, 15:04
08 апр 2026, 15:04
fcf0a74
Код
Авторство
О чём код?
#!/usr/bin/env bash # Script deletes the specified branch locally and (optionally) on the remote repository set -euo pipefail # Цвета для вывода RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Check if an argument is provided if [ $# -ne 1 ]; then echo -e "${RED}Input error:${NC} branch name is required" echo "" echo "Usage:" echo " gitdel branch-name" echo "" echo "Examples:" echo " gitdel feature/login" echo " gitdel bugfix/123-fix-crash" exit 1 fi BRANCH="$1" CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "") # Check if we are inside a git repository if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then echo -e "${RED}Input error:${NC} current directory is not a git repository" exit 1 fi # Check if the branch exists locally if ! git show-ref --verify --quiet "refs/heads/$BRANCH"; then echo -e "${RED}Input error:${NC} branch ${YELLOW}$BRANCH${NC} not found locally" exit 1 fi # Check if trying to delete the current branch if [ "$BRANCH" = "$CURRENT_BRANCH" ]; then echo -e "${RED}Input error:${NC} cannot delete current branch (${YELLOW}$CURRENT_BRANCH${NC})" echo "Switch to another branch first, for example:" echo " git checkout main" exit 1 fi echo -e "Local branch: ${YELLOW}$BRANCH${NC}" echo -e "Current branch: ${YELLOW}$CURRENT_BRANCH${NC}" echo "" # Delete locally echo -e "Deleting local branch ${YELLOW}$BRANCH${NC}..." if git branch -D "$BRANCH"; then echo -e "${GREEN}Local branch deleted${NC}" else echo -e "${RED}Failed to delete local branch${NC}" exit 1 fi # Determine the main remote (usually origin) REMOTE="origin" if ! git remote | grep -q "^$REMOTE$"; then REMOTE=$(git remote | head -n 1) if [ -z "$REMOTE" ]; then echo -e "${YELLOW}Warning:${NC} no remote repository found" exit 0 fi echo -e "${YELLOW}Using remote:${NC} $REMOTE (auto-detection)" fi # Check if the branch exists on the remote repository if git ls-remote --exit-code --heads "$REMOTE" "$BRANCH" >/dev/null 2>&1; then echo "" echo -e "Branch ${YELLOW}$BRANCH${NC} found in remote repository (${YELLOW}$REMOTE${NC})." read -p "Delete it also on the remote repository? [y/N] " answer case "$answer" in [Yy]* ) echo -e "Deleting remote branch ${YELLOW}$REMOTE/$BRANCH${NC}..." if git push "$REMOTE" --delete "$BRANCH"; then echo -e "${GREEN}Remote branch deleted successfully${NC}" else echo -e "${RED}Error deleting remote branch${NC}" echo "Try manually:" echo " git push $REMOTE --delete $BRANCH" fi ;; * ) echo -e "${YELLOW}Remote branch NOT deleted${NC}" ;; esac else echo -e "${YELLOW}Branch $BRANCH not found in remote repository ($REMOTE)${NC}" fi echo "" echo -e "${GREEN}Done.${NC}"