一、首次推送内容到Git仓库
1.1 基础步骤(手动操作)
bash
# 步骤1:进入项目目录 cd /path/to/your/project # 步骤2:初始化Git仓库 git init # 步骤3:添加所有文件到暂存区 git add . # 步骤4:提交更改 git commit -m "初始提交:项目首次推送" # 步骤5:添加远程仓库地址 git remote add origin https://git.example.com/your-team/your-project.git # 步骤6:推送到远程仓库 git push -u origin main
1.2 首次推送脚本(带示例配置)
bash
#!/bin/bash
# first_push.sh - 首次推送脚本
echo "🚀 开始首次推送..."
# 配置信息(根据实际情况修改)
PROJECT_DIR="."
REPO_URL="https://git.example.com/your-team/your-project.git"
BRANCH="main"
COMMIT_MSG="初始提交:U8接口自动化测试脚本"
cd "$PROJECT_DIR"
# 检查是否已经是Git仓库
if [ -d ".git" ]; then
echo "⚠️ 当前目录已经是Git仓库"
read -p "是否继续?(y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "❌ 操作已取消"
exit 0
fi
fi
# 初始化Git仓库
echo "1. 初始化Git仓库..."
git init
# 配置用户信息
git config user.email "developer@example.com"
git config user.name "Your Name"
# 添加所有文件
echo "2. 添加所有文件..."
git add .
# 提交更改
echo "3. 提交更改..."
git commit -m "$COMMIT_MSG"
# 添加远程仓库
echo "4. 连接远程仓库..."
git remote add origin "$REPO_URL"
# 推送
echo "5. 推送到远程仓库..."
if git push -u origin "$BRANCH"; then
echo ""
echo "✅ 首次推送完成!"
else
echo "❌ 推送失败,请检查网络和权限"
fi
二、持续推送修改或新增内容
2.1 日常更新工作流
bash
# 日常更新三步曲 git add . git commit -m "更新说明" git push origin 分支名
2.2 智能更新脚本
bash
#!/bin/bash
# daily_push.sh - 日常更新推送脚本
echo "📦 Git日常更新推送"
# 获取当前分支
CURRENT_BRANCH=$(git branch --show-current)
COMMIT_MSG="更新: $(date '+%Y-%m-%d %H:%M:%S')"
# 检查是否有更改
if git diff --quiet && git diff --cached --quiet; then
echo "📭 没有检测到更改"
exit 0
fi
# 显示更改摘要
echo "📋 更改摘要:"
git status --short
# 添加并提交
git add .
git commit -m "$COMMIT_MSG"
# 推送
echo "🚀 推送到远程仓库..."
if git push origin "$CURRENT_BRANCH"; then
echo "✅ 更新推送完成!"
else
echo "❌ 推送失败,可能需要先拉取远程更新"
fi
三、常见推送问题及解决方案
3.1 问题一:本地分支不存在
错误信息:
text
error: src refspec V11_2507 does not match any
解决方案:
bash
# 创建本地分支 git checkout -b V11_2507 # 添加并提交文件 git add . git commit -m "创建分支 V11_2507" # 推送到远程 git push -u origin V11_2507
3.2 问题二:远程包含本地没有的更改
错误信息:
text
! [rejected] V11_2507 -> V11_2507 (fetch first) error: failed to push some refs hint: Updates were rejected because the remote contains work that you


8万+

被折叠的 条评论
为什么被折叠?



