Git删除过去分支(如删除23年及之前的分支)

下面这个shell清理23年及之前的分支,在清理之前将分支显示在"branch_dates.txt"文件中

shell 复制代码
#!/bin/bash

echo "开始获取远程分支的创建时间信息..."

# 获取所有远程分支
branches=$(git branch -r | grep -v HEAD | sed 's/origin\///')

# 临时文件存储分支信息
temp_file="branch_dates.txt"
> $temp_file

# 获取每个分支的最后提交时间
echo "正在分析分支创建时间..."
for branch in $branches; do
    if [[ "$branch" == "master" ]]; then
        continue  # 跳过master分支
    fi
    
    # 获取分支的最后提交时间
    last_commit_date=$(git log --pretty=format:"%ci" origin/$branch -1 2>/dev/null)
    
    if [[ -n "$last_commit_date" ]]; then
        # 提取日期部分 (YYYY-MM-DD)
        commit_date=$(echo $last_commit_date | cut -d' ' -f1)
        echo "$branch:$commit_date" >> $temp_file
    fi
done

# 显示2023年及之前的分支
echo -e "\n=== 2023年及之前的分支 ==="
cutoff_date="2023-12-31"
old_branches=()

while IFS=: read -r branch date; do
    if [[ "$date" < "$cutoff_date" ]] || [[ "$date" == "$cutoff_date" ]]; then
        echo "$branch (最后提交: $date)"
        old_branches+=("$branch")
    fi
done < $temp_file

echo -e "\n共找到 ${#old_branches[@]} 个2023年及之前的分支"

# 询问是否删除
echo -e "\n是否要删除这些分支? (yes/no)"
read -r response

if [[ "$response" == "yes" ]]; then
    echo "开始删除旧分支..."
    for branch in "${old_branches[@]}"; do
        echo "删除分支: $branch"
        git push origin --delete "$branch" 2>/dev/null
        if [[ $? -eq 0 ]]; then
            echo "✓ 成功删除: $branch"
        else
            echo "✗ 删除失败: $branch"
        fi
    done
    echo "删除完成!"
else
    echo "取消删除操作"
fi

# 清理临时文件
rm -f $temp_file
相关推荐
不老刘12 小时前
Antigravity 的 New Worktree 模式:给 AI 代理一个安全的“平行工位”
人工智能·git·ai编程·antigravity
光谱SpectrAI13 小时前
Windows 也中招:Git Bash 和 Node 仍信任泄露的 eMudhra 证书
git
YIN_尹13 小时前
【git原理与使用】git初识与安装
大数据·git·elasticsearch
Leo_Mr18 小时前
Vue3 项目配置 Git 提交规范(企业通用方案:husky + commitlint + cz-git + lint-staged)
git
jyOverQ18 小时前
Git rebase 和 merge 有什么区别?什么时候该用哪个?
git
晗夜未央18 小时前
跨仓库 Skill 换台电脑就失效?用 Git remote 校验项目
人工智能·git·python
Lsetea18 小时前
Git访问HTTPS证书不受信任:用URL级sslCAInfo限定私有CA范围
git·https·开发工具·ssl证书·ca证书
এ慕ོ冬℘゜1 天前
Git 基础入门|和 SVN 对比 + 四大区域 + 常用命令详解
git·svn
DevLoom_2 天前
macOS 命令行 curl/git 报 SSL 证书错误?eMudhra 事件完整修复指南
git·macos·ssl
keyipatience2 天前
Git学习(4)Git分布式优势与实战配置全解析
git·学习