批量清除本地分支

批量清除本地分支在多个项目频繁切换 快速迭代过程中 我们在本地开发环境创建过许多 feature bugfix 分支没有清理

大家好,欢迎来到IT知识分享网。

背景

在多个项目频繁切换、快速迭代过程中,我们在本地开发环境创建过许多feature、bugfix分支没有清理。我们期望能够提供一键清空本地无用分支的脚本。

脚本功能

  1. 展示当前目录下所有仓库缓存信息
  2. 删除当前目录下所有仓库缓存信息
  3. 展示当前目录下所有仓库分支
  4. 切换至目标分支并拉取最新代码
  5. 删除所有其他本地分支

功能演示

windows下.bat文件操作小demo演示图片

在这里插入图片描述

linux下.sh文件操作大demo演示视频

https://live.csdn.net/v/

实现

核心:批量操作+自定义指令

bat脚本

@REM 目标:清理本地开发环境 @REM 删除本地所有分支和stash缓存,保留${targetBranch}分支 @echo off setlocal EnableDelayedExpansion echo Welcome to the roubing999 tool... echo. set dp=%~dp0 setlocal set yn=n set /p yn="Whether to show the cache in stash (y | n)? " call :confirm !yn! echo. if !yn!==y ( call :show_stashes %CD% ) echo -----查看当前目录下所有仓库中的stash缓存数据完成----- endlocal setlocal set yn=n set /p yn="Whether to delete the cache in stash (y | n)? " call :confirm !yn! echo. if !yn!==y ( call :clear_stashes %CD% ) echo -----批量删除当前目录下所有仓库中的stash缓存数据完成----- endlocal setlocal set yn=n set /p yn="Whether to show all branches (y | n)? " call :confirm !yn! echo. if !yn!==y ( call :show_branches %CD% echo -----查看当前目录下的所有仓库分支完成----- ) endlocal setlocal set yn=n set /p yn="Whether to continue the batch delete (y | n)? " call :confirm !yn! if !yn!==n ( echo exit. exit ) echo. endlocal setlocal set branch=develop set /p branch="Please enter the target branch (develop): " echo target branch: %branch% set now=%date% %time% set now=!now: =-! echo now: %now% call :switch_and_update_branch %CD% %branch% %now% echo -----批量删除本地分支完成----- endlocal echo. echo Finished. exit :show_branches for /d %%i in (%1\*) do ( cd %%i if exist .git ( set p=%%i echo The branches under the repo path !p:%dp%=.\! git branch ) else ( call :show_branches %%i ) ) goto :EOF :switch_and_update_branch for /d %%i in (%1\*) do ( cd %%i if exist .git ( set p=%%i echo. echo current path: !p:%dp%=.\! git stash save %3 set hasTargetBranch=n for /f "delims=" %%j in ('git branch') do ( echo %%j | findStr %2 > nul && set hasTargetBranch=y ) if !hasTargetBranch!==y ( git switch %2 ) else ( git remote update origin --prune git checkout -b develop origin/develop ) git branch | xargs git branch -D git pull ) else ( call :switch_and_update_branch %%i %2 %3 ) ) goto :EOF :show_stashes for /d %%i in (%1\*) do ( cd %%i if exist .git ( set p=%%i echo current repo path !p:%dp%=.\! git stash list ) else ( call :show_stashes %%i ) ) goto :EOF :clear_stashes for /d %%i in (%1\*) do ( cd %%i if exist .git ( set p=%%i echo current repo path !p:%dp%=.\! git stash clear ) else ( call :clear_stashes %%i ) ) goto :EOF :confirm if !yn!==y ( echo. ) else if !yn!==n ( echo. ) else if !yn!==-1 ( echo exit. exit ) else ( echo error. set /p yn="input error value: !yn!, please re-enter (y | n): " call :confirm !yn! ) goto :EOF 

shell脚本

# 目标:清理本地开发环境 # 删除本地所有分支和stash缓存,保留${targetBranch}分支 #!/bin/bash echo Welcome to the roubing999 tool... function confirm { if [[ $yn == "y" ]] then return elif [[ $yn == "n" || $yn == "" ]] then yn="n" elif [[ $yn == -1 ]] then exit else echo -e "\033[31m error \033[0m" read -p "input error value: ${yn}, please re-enter (y | n): " yn confirm yn fi } function show_stashes { for path in $(ls $1) do if test -d $1/${path} then if test -e $1/$path/.git then echo -e "\033[34m current repo path $1/$path\033[0m" cd $1/$path git stash list else show_stashes $1/${path} fi fi done } function clear_stashes { for path in $(ls $1) do if test -d $1/${path} then if test -e $1/$path/.git then echo -e "\033[34m current repo path $1/$path\033[0m" cd $1/$path git stash clear else clear_stashes $1/${path} fi fi done } function show_branches { for path in $(ls $1) do if test -d $1/${path} then if test -e $1/$path/.git then echo -e "\033[34m current repo path $1/$path\033[0m" cd $1/$path git branch else show_branches $1/${path} fi fi done } function switch_and_update_branch { for path in $(ls $1) do if test -d $1/${path} then if test -e $1/$path/.git then echo -e "\033[34m current repo path $1/$path\033[0m" cd $1/$path git stash save $3 hasTargetBranch="n" for branch in $(git branch) do if [[ $branch == $2 ]] then hasTargetBranch="y" fi done if [[ $hasTargetBranch == "y" ]] then git switch $2 else # 在本地未找到目标分支,则尝试更新与远程的关联,切换到默认目标分支 git remote update origin --prune git checkout -b develop origin/develop fi git branch | xargs git branch -D git pull else switch_and_update_branch $1/${path} $2 $3 fi fi done } # 主流程 CURRENT_DIR=$(cd `dirname $0`; pwd) read -p "Whether to show the cache in stash (y | n)? " yn confirm yn if [[ $yn == "y" ]] then show_stashes $CURRENT_DIR echo -e "\033[32m -----查看当前目录下所有仓库中的stash缓存数据完成-----\033[0m \033[34m" fi read -p "Whether to delete the cache in stash (y | n)? " yn confirm yn if [[ $yn == "y" ]] then clear_stashes $CURRENT_DIR echo -e "\033[32m -----批量删除当前目录下所有仓库中的stash缓存数据完成-----\033[0m \033[34m" fi read -p "Whether to show all branches (y | n)? " yn confirm yn if [[ $yn == "y" ]] then show_branches $CURRENT_DIR echo -e "\033[32m -----查看当前目录下的所有仓库分支完成-----\033[0m \033[34m" fi read -p "Whether to continue the batch delete (y | n)? " yn confirm yn if [[ $yn != "y" ]] then exit fi read -p "Please enter the target branch (develop): " branch if [[ $branch == "" ]] then branch="develop" fi echo "target branch: "$branch now=`date +%Y/%m/%d-%H:%M:%S` echo "now: "$now switch_and_update_branch $CURRENT_DIR $branch $now echo -e "\033[32m -----批量删除当前本地分支完成-----\033[0m \033[34m" 

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/134469.html

(0)
上一篇 2025-07-12 12:45
下一篇 2025-07-12 13:00

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信