批量删除远程无效分支的shell脚本

!/bin/bash

shell script delete remote branch and local branch

if [[ "" == 1 || "" ==2 ]]; then
echo "please add reponsitory path and delete date"
echo "example: ./delete_mr.sh ./npu-codebase 365 ###删除超过365天时间的分支"
exit
fi

reponsitory_name="1" echo "You are going to delete【"reponsitory_name"】 branchs !!!!!!";
echo " "
cd reponsitory_name; days="2"

for branch in git branch -r --no-merged | grep -v HEAD;
do

分支名称

branch_name=echo $branch | grep '/' | cut -d '/' -f 2-;

分支最后提交时间

branch_timestamp=git show --format="%ct" $branch | head -n 1;

当前系统时间

cur_sec_and_ns=date '+%s-%N';
cur_sec=${cur_sec_and_ns%-*};

时间差

time_different=[cur_sec-branch_timestamp];

阈值g

time_require=[3600*24*days];

if [[ "" !=  "$branch_name"
        && "HEAD" != "$branch_name" 
        && "master" != "$branch_name"
        && $time_different -ge $time_require
        ]]; then
    echo "deleting current branch: " `git show --format="%ci %cr" $branch | head -n 1` $branch_name
    echo "branch_name: " $branch_name
    echo "delete local "$branch_name
    local_result=`git branch -d $branch_name`
    echo "delete remote "$branch_name
    remote_result=`git push origin --delete $branch_name`
    echo "delete over"
    echo ""
fi          

done
echo "!!!!! game over !!!!!";

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容