GIT批量拉取代码BAT

一键拉取代码

如果我们的项目比较多,需要一个一个更新代码,我们只能一个目录进入来pull,太麻烦了,这里提供一个一键拉取代码的脚本。

支持当前同级目录项目及子目录项目的拉取更新。

pull_batch.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
@echo off  
setlocal EnableDelayedExpansion

set MYPATH=%cd%
for /d %%a in (%MYPATH%\*) do (
echo %%a|findstr "pass" >nul
if not !errorlevel! equ 0 (
cd %%a
if exist %%a\.git\ (
echo %%a
git pull
echo ------------------------------------------------
) else (
for /d %%b in (%%a\*) do (
echo %%b|findstr "pass" >nul
if not !errorlevel! equ 0 (
cd %%b
if exist %%b\.git\ (
echo %%b
git pull
echo ------------------------------------------------
)
cd ..
)
)
)
cd ..
)

)
pause

这样把该文件放在项目文件夹下,双击运行即可。

拉取代码(优化)

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87

@echo off
setlocal EnableDelayedExpansion

:: 配置参数
set MYPATH=%cd%
set VERBOSE=0 :: 0=简洁模式, 1=详细模式
set EXCLUDE_DIRS=pass :: 要排除的目录名

:: 颜色定义
for /f "delims=#" %%a in ('"prompt #$E# & for %%b in (1) do rem"') do set "ESC=%%a"
set "COLOR_RED=%ESC%[91m"
set "COLOR_GREEN=%ESC%[92m"
set "COLOR_YELLOW=%ESC%[93m"
set "COLOR_RESET=%ESC%[0m"

:: 主函数
call :main
pause
exit /b

:main
for /d %%a in ("%MYPATH%\*") do (
echo %%a|findstr /i "%EXCLUDE_DIRS%" >nul
if !errorlevel! neq 0 (
call :process_repo "%%a"
)
)
exit /b

:process_repo
set "repo_path=%~1"
pushd "!repo_path!"

if exist .git (
call :get_git_info
call :check_repo_status
call :update_repo
) else (
for /d %%b in ("!repo_path!\*") do (
echo %%b|findstr /i "%EXCLUDE_DIRS%" >nul
if !errorlevel! neq 0 (
call :process_repo "%%b"
)
)
)

popd
exit /b

:get_git_info
for /f "delims=" %%b in ('git rev-parse --abbrev-ref HEAD 2^>nul') do set "branch=%%b"
for /f "delims=" %%c in ('git config --get remote.origin.url 2^>nul') do set "remote_url=%%c"
exit /b

:check_repo_status
echo.
echo %COLOR_GREEN%[检查仓库] %repo_path%%COLOR_RESET%
echo 分支: %branch%
echo 远程: %remote_url%

git status --porcelain | findstr /r "^ M ^A ^D ^R ^C ^U" >nul
if !errorlevel! equ 0 (
echo %COLOR_YELLOW% [警告] 存在未提交的修改%COLOR_RESET%
git status --short

call :submit_repo
)

git fetch --dry-run 2>&1 | findstr /i "updating" >nul
if !errorlevel! equ 0 (
echo %COLOR_YELLOW% [警告] 远程有更新%COLOR_RESET%
)
exit /b

:submit_repo
echo %COLOR_GREEN% [提交仓库]%COLOR_RESET%
git add .
git commit -m"submit"
git push
exit /b

:update_repo
echo %COLOR_GREEN% [更新仓库]%COLOR_RESET%
git pull
echo ------------------------------------------------
exit /b