-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete_local_branches_without_remote.bat
71 lines (63 loc) · 2.09 KB
/
delete_local_branches_without_remote.bat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
@echo off
setlocal enabledelayedexpansion
@REM Store the initial path from where the script was exec
set "init_path=%cd%"
REM Prompt the user for the path to their Git repository
set /p repo_path="Enter the path to your Git repository: "
REM Change to the specified repository directory
cd /d "%repo_path%"
set "local_branches="
REM Define an array to store the names of local branches
for /f "delims=" %%i in ('git branch') do (
if "%%i" NEQ "*" (
set "branch=%%i"
REM echo The branch is: !branch!
set branch=!branch:~2!
echo The branch found is: !branch!
set "local_branches=!local_branches! !branch!"
)
)
REM echo The local branches are: %local_branches%
echo.
pause
REM Check if there are any local branches
set "branch_name="
if defined local_branches (
REM Loop through the local branches
for %%b in (!local_branches!) do (
REM Check if the branch has a remote counterpart
git show-ref --verify --quiet "refs/remotes/origin/%%b"
REM echo %errorlevel%
if errorlevel 1 (
REM If not, ask the user if they want to delete the branch
set "branch_name=%%b"
set /p choice= "Do you want to delete the local branch "%%b" without a remote counterpart? [y/n]: "
echo.
pause
echo "!choice!" Proceed to deletion ...
if /i "!choice!"=="y" (
git branch -d "%%b"
if errorlevel 0 (
echo.
echo Deleted local branch "%%b" without a remote counterpart.
) else (
echo Failed to delete local branch %%b without a remote counterpart...checking issue...
@REM git checkout "%%b"
@REM if errorlevel 1 (
@REM echo.
@REM echo Could not switch to branch "%%b". Skipping...
@REM echo Because already on "%%b". Skipping...
@REM )
@REM @REM continue
)
)
) else (
echo.
echo No local branches without remote counterparts found.
)
)
) else (
echo No local branches found.
)
cd /d %init_path%
endlocal