Clone
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.
以下是同步全部远程分支的 Git 命令:
1. 获取所有远程分支信息
# 获取远程所有分支信息,不合并
git fetch --all
# 或更详细的获取
git fetch origin
2. 查看所有分支(包括远程)
# 查看本地分支
git branch
# 查看远程分支
git branch -r
# 查看所有分支(本地和远程)
git branch -a
3. 同步并创建本地分支对应所有远程分支
# 方法1:逐个同步(推荐)
git fetch --all
git branch -r | grep -v '\->' | while read remote; do
git branch --track "${remote#origin/}" "$remote" 2>/dev/null || true
done
# 方法2:使用脚本(更安全)
for branch in $(git branch -r | grep -v '\->'); do
git branch --track "${branch#origin/}" "$branch" 2>/dev/null || true
done
4. 一键同步所有远程分支的常用方法
方法A:使用 fetch 和 checkout
# 获取所有远程分支
git fetch --all
# 查看远程分支列表
git branch -r
# 对于每个需要的远程分支创建本地分支
git checkout -b feature-branch origin/feature-branch
方法B:使用 shell 脚本
创建脚本 sync-all-branches.sh:
#!/bin/bash
# 获取所有远程分支
git fetch --all --prune
# 遍历所有远程分支
for branch in $(git branch -r | grep -v '\->' | grep -v 'HEAD'); do
# 提取分支名(去掉 origin/ 前缀)
local_branch=${branch#origin/}
# 检查本地是否已存在该分支
if ! git show-ref --verify --quiet refs/heads/"$local_branch"; then
echo "创建分支: $local_branch"
git branch --track "$local_branch" "$branch"
else
echo "分支已存在: $local_branch"
fi
done
运行脚本:
chmod +x sync-all-branches.sh
./sync-all-branches.sh
方法C:单行命令
# 获取所有分支并在本地创建跟踪分支
git fetch --all && for b in `git branch -r | grep -v -- '->'`; do git branch --track ${b##origin/} $b; done
# 或者更安全的版本(忽略已存在的分支)
git fetch --all && git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote" 2>/dev/null; done || true
5. 更新所有已存在的本地分支
# 方法1:切换到每个分支并拉取
git branch | grep -v "*" | while read branch; do
git checkout "$branch"
git pull origin "$branch"
done
# 方法2:更新当前分支和 master/main
git checkout main && git pull origin main
git checkout develop && git pull origin develop
# ... 继续其他分支
6. 清理已删除的远程分支
# 同步时自动清理
git fetch --all --prune
# 或单独清理
git remote prune origin
7. 完整同步流程
# 完整同步脚本
echo "1. 获取所有远程更新..."
git fetch --all --prune
echo "2. 创建本地不存在的远程分支..."
git branch -r | grep -v '\->' | grep -v 'HEAD' | while read remote; do
local_branch=${remote#origin/}
if ! git branch --list "$local_branch" | grep "$local_branch" > /dev/null; then
echo "创建: $local_branch"
git branch --track "$local_branch" "$remote"
fi
done
echo "3. 更新主分支..."
git checkout main 2>/dev/null || git checkout master
git pull origin main 2>/dev/null || git pull origin master
echo "4. 同步完成!"
8. 使用别名简化操作
在 ~/.gitconfig 中添加:
[alias]
sync-all = !git fetch --all --prune && git branch -r | grep -v '\\->' | grep -v 'HEAD' | while read remote; do git branch --track \"${remote#origin/}\" \"$remote\" 2>/dev/null; done || true
update-all = !git branch | grep -v \"*\" | while read branch; do git checkout \"$branch\" && git pull origin \"$branch\"; done && git checkout -
使用别名:
# 同步所有远程分支到本地
git sync-all
# 更新所有本地分支
git update-all
9. 注意事项
安全同步
# 先查看有哪些远程分支
git branch -r
# 只同步需要的分支,避免同步过多分支
git fetch origin branch1 branch2 branch3
处理冲突
# 如果同步时遇到冲突
git stash # 暂存本地修改
git pull origin branch-name
git stash pop # 恢复修改并解决冲突
推荐的日常同步流程
# 1. 查看状态
git status
# 2. 提交或暂存本地修改
git add .
git commit -m "保存工作"
# 3. 获取远程更新
git fetch --all --prune
# 4. 查看差异
git log HEAD..origin/main --oneline
# 5. 合并或变基
git pull --rebase origin main
10. Windows 批处理脚本
创建 sync-branches.bat:
@echo off
echo 开始同步所有远程分支...
REM 获取所有远程分支
git fetch --all --prune
REM 创建本地分支
for /f "tokens=*" %%b in ('git branch -r ^| findstr /v "->" ^| findstr /v "HEAD"') do (
set remote=%%b
setlocal enabledelayedexpansion
set local_branch=!remote:origin/=!
git show-ref --verify --quiet refs/heads/!local_branch!
if errorlevel 1 (
echo 创建分支: !local_branch!
git branch --track !local_branch! !remote!
) else (
echo 分支已存在: !local_branch!
)
endlocal
)
echo 同步完成!
pause
最常用的简单命令
# 大多数情况下,这几个命令就够了
git fetch --all --prune # 获取并清理
git branch -a # 查看所有分支
git checkout -b 分支名 origin/分支名 # 创建需要的分支
选择适合你工作流程的方法,一般推荐使用 git fetch --all --prune 结合选择性创建本地分支。