一个批量清除Git分支的脚本
首页 专栏 python 文章详情
0

一个批量清除Git分支的脚本

遁壹 发布于 2 月 27 日

每开发一个新功能,往往都会新建一个功能分支。

久而久之,项目的本地代码仓库上,就会累积较多的过时分支。

若要逐一手动清理这些过时分支,想想都觉得累。为了更高效的摸鱼,写了一个python脚本,用于批量清理过时分支。

假设D:\rui\work\路径下,存在repo-a这一本地代码仓库。在repo-a上,存在着masterfeature-afeature-b等三个分支。现在,想要移除feature-b,可执行如下代码。

值得一提的是,若feature-b存在未push到远端仓库的commit,则feature-b不会被移除。若需强制移除,可以单独执行命令git branch -D feature-b

# 引入第三方库 GitPython
from git import Repo, GitCommandError

# 仓库名称
REPO_NAME = 'repo-a'

# 需要保留的分支,默认保留mater分支
# 注意:没有push新commit到远端仓库的分支,即使不在该集合里,也不会被删除
REMAIN_BRANCH_TUPLE = [
    'feature-a'
]

# 工作路径
WORK_PATH = r'D:\rui\work\\'


def main():
    print('开始啦 0v0\n')

    # 创建版本库对象
    repo_path = WORK_PATH + REPO_NAME
    repo = Repo(repo_path)

    # 若当前分支存在未提交的修改,则中止操作
    if repo.is_dirty():
        print('请先提交当前分支的修改!!!')
        exit()

    # 切换到 master
    repo.heads.master.checkout()

    not_push_branch_list = []
    for head in repo.heads:
        # 分支名不在保留集合中,则删除
        head_name = head.name
        if head_name == 'master' or head_name in REMAIN_BRANCH_TUPLE:
            continue

        try:
            # 移除分支
            # 本质上是执行命令 git branch -d feature-name
            repo.delete_head(head_name)
        except GitCommandError:
            # 未push新commit的分支,执行删除操作,将会抛出GitCommandError异常
            # 当然,如果出现其他错误,也可能会抛出GitCommandError异常。此处,只是简单处理
            not_push_branch_list.append(head_name)

    if not_push_branch_list:
        not_push_branch_str = ', '.join(not_push_branch_list)
        print('没有push新commit的分支: {0}\n'.format(not_push_branch_str))

    print('结束啦 ^0^')


if __name__ == '__main__':
    main()

git python
阅读 56 发布于 2 月 27 日
收藏
分享
本作品系原创, 采用《署名-非商业性使用-禁止演绎 4.0 国际》许可协议
avatar
遁壹

学习、分享、交流、成长

1 声望
0 粉丝
关注作者
0 条评论
得票 时间
提交评论
avatar
遁壹

学习、分享、交流、成长

1 声望
0 粉丝
关注作者
宣传栏
目录

每开发一个新功能,往往都会新建一个功能分支。

久而久之,项目的本地代码仓库上,就会累积较多的过时分支。

若要逐一手动清理这些过时分支,想想都觉得累。为了更高效的摸鱼,写了一个python脚本,用于批量清理过时分支。

假设D:\rui\work\路径下,存在repo-a这一本地代码仓库。在repo-a上,存在着masterfeature-afeature-b等三个分支。现在,想要移除feature-b,可执行如下代码。

值得一提的是,若feature-b存在未push到远端仓库的commit,则feature-b不会被移除。若需强制移除,可以单独执行命令git branch -D feature-b

# 引入第三方库 GitPython
from git import Repo, GitCommandError

# 仓库名称
REPO_NAME = 'repo-a'

# 需要保留的分支,默认保留mater分支
# 注意:没有push新commit到远端仓库的分支,即使不在该集合里,也不会被删除
REMAIN_BRANCH_TUPLE = [
    'feature-a'
]

# 工作路径
WORK_PATH = r'D:\rui\work\\'


def main():
    print('开始啦 0v0\n')

    # 创建版本库对象
    repo_path = WORK_PATH + REPO_NAME
    repo = Repo(repo_path)

    # 若当前分支存在未提交的修改,则中止操作
    if repo.is_dirty():
        print('请先提交当前分支的修改!!!')
        exit()

    # 切换到 master
    repo.heads.master.checkout()

    not_push_branch_list = []
    for head in repo.heads:
        # 分支名不在保留集合中,则删除
        head_name = head.name
        if head_name == 'master' or head_name in REMAIN_BRANCH_TUPLE:
            continue

        try:
            # 移除分支
            # 本质上是执行命令 git branch -d feature-name
            repo.delete_head(head_name)
        except GitCommandError:
            # 未push新commit的分支,执行删除操作,将会抛出GitCommandError异常
            # 当然,如果出现其他错误,也可能会抛出GitCommandError异常。此处,只是简单处理
            not_push_branch_list.append(head_name)

    if not_push_branch_list:
        not_push_branch_str = ', '.join(not_push_branch_list)
        print('没有push新commit的分支: {0}\n'.format(not_push_branch_str))

    print('结束啦 ^0^')


if __name__ == '__main__':
    main()