8.0 KiB
8.0 KiB
非常全面的同步远程分支指南!我来帮你整理几个最实用的方案,并提供更清晰的分类:
📋 最常用的分支同步方案
方案一:基础同步(最常用)
# 1. 获取所有远程更新并清理已删除分支
git fetch --all --prune
# 2. 查看所有分支
git branch -a
# 3. 切换到特定分支并同步
git checkout 分支名
git pull origin 分支名
方案二:一键同步所有分支到本地
# 推荐的安全版本
git fetch --all --prune
for branch in $(git branch -r | grep -v '\->' | grep -v 'HEAD'); do
local_branch=${branch#origin/}
if ! git show-ref --quiet refs/heads/"$local_branch"; then
echo "📦 创建分支: $local_branch"
git branch --track "$local_branch" "$branch"
fi
done
🔧 实用脚本整理
脚本1:智能同步脚本 (sync-branches.sh)
#!/bin/bash
# 智能同步所有远程分支
echo "🔄 开始同步分支..."
# 1. 获取所有远程更新
git fetch --all --prune
# 2. 同步主要分支
main_branch=""
if git show-ref --verify --quiet refs/remotes/origin/main; then
main_branch="main"
elif git show-ref --verify --quiet refs/remotes/origin/master; then
main_branch="master"
fi
if [ -n "$main_branch" ]; then
echo "📌 更新主分支: $main_branch"
git checkout $main_branch 2>/dev/null || git checkout -b $main_branch origin/$main_branch
git pull origin $main_branch
fi
# 3. 创建本地不存在的远程分支
echo "📦 同步其他分支..."
git branch -r | grep -v '\->' | grep -v 'HEAD' | while read remote; do
local_branch=${remote#origin/}
# 跳过已经存在的分支
if git show-ref --verify --quiet refs/heads/"$local_branch"; then
echo " ✓ 已存在: $local_branch"
continue
fi
# 创建跟踪分支
echo " ➕ 创建: $local_branch"
git branch --track "$local_branch" "$remote" 2>/dev/null
done
# 4. 检查是否需要更新现有分支
echo "🔄 检查本地分支更新..."
current_branch=$(git branch --show-current)
git branch --format='%(refname:short)' | while read branch; do
if [ "$branch" != "$current_branch" ]; then
git checkout "$branch" >/dev/null 2>&1
if [ $? -eq 0 ]; then
git pull origin "$branch" --ff-only >/dev/null 2>&1
echo " ⬆️ 已更新: $branch"
fi
fi
done
# 5. 返回原分支
git checkout "$current_branch" >/dev/null 2>&1
echo "✅ 同步完成!"
echo "📊 分支统计:"
git branch -a | grep -c "remotes/origin" | xargs echo "远程分支数: "
git branch | wc -l | xargs echo "本地分支数: "
脚本2:快速切换和同步 (git-sync)
#!/bin/bash
# 快速切换到指定分支并同步
if [ $# -eq 0 ]; then
echo "用法: git-sync 分支名"
exit 1
fi
BRANCH=$1
echo "🔄 切换到分支: $BRANCH"
# 检查分支是否存在
if git show-ref --verify --quiet refs/heads/"$BRANCH"; then
# 本地分支存在
git checkout "$BRANCH"
git pull origin "$BRANCH"
elif git show-ref --verify --quiet refs/remotes/origin/"$BRANCH"; then
# 远程分支存在,创建本地跟踪分支
git checkout -b "$BRANCH" origin/"$BRANCH"
else
echo "❌ 错误: 分支 $BRANCH 不存在"
echo "可用的远程分支:"
git branch -r | grep -v '\->' | sed 's/origin\///' | sort | uniq
exit 1
fi
echo "✅ 完成!当前分支: $(git branch --show-current)"
⚙️ Git 配置别名(推荐)
添加到 ~/.gitconfig 的 [alias] 部分:
[alias]
# 同步所有分支
sync-all = "!f() { \
echo '🔄 获取远程更新...'; \
git fetch --all --prune; \
echo '📦 创建本地分支...'; \
git branch -r | grep -v '\\->' | grep -v 'HEAD' | while read remote; do \
local=${remote#origin/}; \
if ! git show-ref --quiet refs/heads/\"$local\"; then \
echo \" ➕ $local\"; \
git branch --track \"$local\" \"$remote\" 2>/dev/null; \
fi; \
done; \
echo '✅ 同步完成!'; \
}; f"
# 更新当前仓库所有分支
update-all = "!f() { \
current=$(git branch --show-current); \
for branch in $(git branch --format='%(refname:short)'); do \
if [ \"$branch\" != \"$current\" ]; then \
git checkout \"$branch\" 2>/dev/null && git pull origin \"$branch\" 2>/dev/null; \
fi; \
done; \
git checkout \"$current\"; \
echo '✅ 所有分支已更新!'; \
}; f"
# 查看分支树状图
tree = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all
# 清理已合并的分支
clean-branches = "!f() { \
git fetch --all --prune; \
git branch --merged main | grep -v '^\\* main$' | xargs -n 1 git branch -d; \
echo '🧹 已清理合并到main的分支'; \
}; f"
使用别名:
git sync-all # 同步所有远程分支到本地
git update-all # 更新所有本地分支
git tree # 查看分支树状图
git clean-branches # 清理已合并的分支
🎯 针对不同场景的推荐方案
场景1:新电脑初次同步
# 1. 克隆仓库
git clone ssh://renjianbo@101.43.95.130:29418/zhini_im
# 2. 进入目录并同步所有分支
cd zhini_im
git sync-all # 使用上面配置的别名
场景2:日常更新
# 推荐的工作流程
git status # 1. 查看状态
git stash # 2. 暂存未提交的修改(可选)
git fetch --all --prune # 3. 获取最新状态
git checkout main # 4. 切换到主分支
git pull --rebase origin main # 5. 更新主分支
git checkout 你的分支 # 6. 切回你的分支
git rebase main # 7. 基于最新main重定基
git stash pop # 8. 恢复暂存的修改
场景3:清理分支
# 查看哪些分支已经合并到main
git branch --merged main
# 删除已合并的分支(除了当前分支和main)
git branch --merged main | grep -v "main" | xargs git branch -d
# 强制删除未合并的分支
git branch -D 分支名
# 删除远程分支(谨慎操作)
git push origin --delete 分支名
📊 分支管理最佳实践
分支命名规范
feature/ # 新功能
bugfix/ # 修复bug
hotfix/ # 紧急修复
release/ # 发布分支
常用命令速查
# 查看分支关系
git log --graph --oneline --all
# 查看远程分支的最后提交
git for-each-ref --sort=-committerdate refs/remotes/origin/ --format='%(refname:short) - %(committerdate:short)'
# 查看分支差异
git diff main..你的分支 # 比较差异
git log main..你的分支 # 查看提交记录差异
# 重命名分支
git branch -m 旧名字 新名字 # 本地重命名
git push origin :旧名字 # 删除远程旧分支
git push origin 新名字 # 推送新分支
git push origin -u 新名字 # 设置上游分支
🚨 注意事项
- 备份重要修改:同步前先提交或暂存工作
- 小心 force push:避免使用
--force,用--force-with-lease更安全 - 定期清理:删除已合并的本地分支,保持仓库整洁
- 沟通协调:多人协作时,删除远程分支前要确认
🎪 一个命令完成所有同步
终极简化版:
# 创建 ~/bin/git-full-sync 文件
#!/bin/bash
cd /path/to/your/project
git stash
git fetch --all --prune
git checkout main && git pull origin main
git sync-all # 使用别名
git stash pop
echo "✅ 全量同步完成!"
# 添加执行权限
chmod +x ~/bin/git-full-sync
# 使用
cd 你的项目
git-full-sync
这些方案覆盖了大部分使用场景,根据你的具体需求选择合适的方法即可。最常用的是方案一的基础同步,配合别名可以极大提高效率。