Files
assist/fix_git_push.bat

126 lines
2.7 KiB
Batchfile
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
@echo off
chcp 65001 >nul
echo ========================================
echo Git推送问题诊断和修复工具
echo ========================================
echo.
:: 1. 检查Git状态
echo [1] 检查Git状态...
git status
if %errorlevel% neq 0 (
echo ? Git未初始化
pause
exit /b 1
)
echo.
:: 2. 检查远程仓库配置
echo [2] 检查远程仓库配置...
git remote -v
if %errorlevel% neq 0 (
echo ? 未配置远程仓库
echo 请先运行: git remote add origin <仓库地址>
pause
exit /b 1
)
echo.
:: 3. 检查当前分支
echo [3] 检查当前分支...
git branch --show-current
echo.
:: 4. 检查是否有未提交的更改
echo [4] 检查未提交的更改...
git status --porcelain
if %errorlevel% equ 0 (
echo ?? 有未提交的更改
set /p commit="是否先提交更改? (y/n): "
if /i "%commit%"=="y" (
git add .
set /p msg="请输入提交信息: "
if "%msg%"=="" set msg=自动提交
git commit -m "%msg%"
)
)
echo.
:: 5. 尝试获取远程分支信息
echo [5] 获取远程分支信息...
git fetch origin
if %errorlevel% neq 0 (
echo ? 无法连接到远程仓库
echo.
echo 可能的原因:
echo 1. 网络连接问题
echo 2. 远程仓库地址错误
echo 3. 需要认证请检查是否已配置SSH密钥或Token
echo.
echo 远程仓库地址:
git config --get remote.origin.url
pause
exit /b 1
)
echo ? 远程仓库连接成功
echo.
:: 6. 检查分支跟踪关系
echo [6] 检查分支跟踪关系...
git branch -vv
echo.
:: 7. 尝试推送到远程
echo [7] 尝试推送...
set current_branch=
for /f "tokens=*" %%b in ('git branch --show-current') do set current_branch=%%b
echo 当前分支: %current_branch%
echo.
:: 检查远程是否存在该分支
git ls-remote --heads origin %current_branch% >nul 2>&1
if %errorlevel% equ 0 (
echo 远程分支 %current_branch% 已存在
echo.
echo 尝试使用当前分支名称推送...
git push origin %current_branch%
) else (
echo 远程分支 %current_branch% 不存在
echo.
echo 尝试设置上游并推送...
git push -u origin %current_branch%
)
if %errorlevel% equ 0 (
echo.
echo ? 推送成功!
) else (
echo.
echo ? 推送失败
echo.
echo ? 常见问题和解决方案:
echo.
echo 1. 如果是认证问题:
echo - 检查SSH密钥: ssh -T git@github.com (GitHub) 或 ssh -T git@gitee.com (Gitee)
echo - 或使用HTTPS + Token方式
echo.
echo 2. 如果是分支冲突:
echo - 运行: git pull origin %current_branch% --rebase
echo - 解决冲突后: git push origin %current_branch%
echo.
echo 3. 如果远程分支名称不同:
echo - 检查远程分支: git branch -r
echo - 可能需要推送主分支: git push origin main 或 git push origin master
echo.
pause
exit /b 1
)
echo.
echo ========================================
echo ? 诊断完成!
echo ========================================
pause