git 不同仓库 同步

文章详细描述了一段bash脚本,用于在复深蓝代码和上海证券代码库中执行克隆、更新、切换不同分支以及拷贝代码的操作,涉及到gitpull,checkout,merge和push等命令。脚本根据不同参数执行针对不同分支的相应操作。

参数1:1:克隆分支 2:更新分支 3:切换分支 4:拷贝代码

if_git_pull_or_checkoutBranch=$1

##参数2: 复深蓝代码分支
fulan_branch=$2

##参数2: 项目名称
project_name=$2

##参数3: 上海证券代码分支
securities_branch=$3

#复深蓝代码路径
fulanlinuxPath=‘/d/git-work/fulan’;

上海证券代码路径

securitieslinuxPath=‘/d/git-work/sh-securities’;

#开启扩展通配符
shopt -s extglob

##参数2: 复深蓝代码分支

fulan_branch1=$2

fulan_branch2=$3

fulanArrays=(
‘cloud-base’
‘cloud-knowledge’
‘cloud-xxljob-client’
‘cloud-system’
‘cloud-workflow’
‘cloud-gateway’
‘cloud-parent’
‘cloud-people’
‘cloud-project’
‘front-pc’
‘front-manage’
‘front-h5’
)

   # 克隆分支

if [ $if_git_pull_or_checkoutBranch -eq 1 ]; then
rm -rf $securitieslinuxPath/* fulanlinuxPath/∗forvaluein"fulanlinuxPath/* for value in "fulanlinuxPath/forvaluein"{fulanArrays[@]}"
do
cd $fulanlinuxPath

  if [[ $value =~ 'front' ]]
  then  git clone http://git.biandejun.com/sh-securities-group/front-space/$value.git
  else  git clone http://git.biandejun.com/sh-securities-group/$value.git 
  fi

cp -r ls | grep -v .git | xargs linuxPath

  cd $securitieslinuxPath
  git clone http://10.4.2.100:31101/app_dev/mss/$value.git
done

更新分支

elif [ $if_git_pull_or_checkoutBranch -eq 2 ]; then

#开启模式匹配功能
shopt -s extglob
for value in “${fulanArrays[@]}”
do
cd $fulanlinuxPath
cd $value
git pull
echo -e “\033[0;32m git pull 复深蓝 path = fulanlinuxPath/fulanlinuxPath/fulanlinuxPath/value \033[0m”
cd $securitieslinuxPath
cd $value
git pull
echo -e “\033[0;36m git pull 上海证券 path = securitieslinuxPath/securitieslinuxPath/securitieslinuxPath/value \033[0m”
done

# 切换分支  复深蓝代码分支    上海证券代码分支
elif [ $if_git_pull_or_checkoutBranch -eq 3 ]; then
    for value in "${fulanArrays[@]}"
do
  cd $fulanlinuxPath
  cd $value
  git fetch
  git pull
  git checkout $fulan_branch
   git pull
  echo -e "\033[0;32m git checkout 复深蓝 $value branch $fulan_branch \033[0m"
  cd $securitieslinuxPath
  cd $value
  git fetch
  git pull
  git checkout $securities_branch
  git pull
  echo -e "\033[0;36m git checkout 上海证券 $value branch $securities_branch \033[0m"
done

 # 拷贝所有代码
elif [ $if_git_pull_or_checkoutBranch -eq 4 ]; then
    for value in "${fulanArrays[@]}"
do

  cd $fulanlinuxPath
  cd $value
  git pull
  echo -e "\033[0;36m pull 复深蓝 $value \033[0m"


  cd $securitieslinuxPath
  cd $value
  echo -e "\033[0;36m pull 上证 $value \033[0m"


#cloud-parent工程pom.xml手动管理
  if [[ $value =~ 'parent' ]]
  then ls $fulanlinuxPath/$value/ | grep -v 'pom.xml\|.git' | xargs -i cp -rf $fulanlinuxPath/$value/{} $securitieslinuxPath/$value/

#前端工程除了.git全部替换
  elif [[ $value =~ 'front' ]]
  then
 
  rm -rf !(.git)
  ls -A $fulanlinuxPath/$value/ | grep -v '.git' | xargs -i cp -rf $fulanlinuxPath/$value/{} $securitieslinuxPath/$value/

#后端工程删除替换指定文件/目录
  else 
    rm -rf Dockerfile pom.xml src
    ls  $fulanlinuxPath/$value/ | grep -v '.git' | xargs -i cp -rf $fulanlinuxPath/$value/{} $securitieslinuxPath/$value/
   fi
 
  echo -e "\033[0;36m ls $fulanlinuxPath/$value/ | grep -v '.git' | xargs -i cp -r $fulanlinuxPath/$value/{} $securitieslinuxPath/$value/  \033[0m"
  
  cd $securitieslinuxPath
  cd $value
  git pull
    ls -all
  git add .
  git commit -m "fsl合并代码 `date` "
  git push

done

  # 拷贝单个代码
elif [ $if_git_pull_or_checkoutBranch -eq 5 ]; then
    

  cd $fulanlinuxPath
   echo -e "\033[0;36m cd $fulanlinuxPath  \033[0m"
  cd $project_name
  echo -e "\033[0;36m cd $project_name  \033[0m"
  basepath=$(pwd)
  echo -e "\033[0;36m 当前路径:$basepath  \033[0m"
  git pull
  echo -e "\033[0;36m pull 复深蓝 $project_name \033[0m"
 ## rm -rf Dockerfile pom.xml src

  cd $securitieslinuxPath
  echo -e "\033[0;36m cd $securitieslinuxPath  \033[0m"
  cd $project_name
  	  echo -e "\033[0;36m cd $project_name  \033[0m"
	  basepath=$(pwd)
  echo -e "\033[0;36m 当前路径:$basepath  \033[0m"
  git pull
   echo -e "\033[0;36m pull 上海证券 $project_name \033[0m"
   
   
if [[ $project_name =~ 'parent' ]]
  then ls $fulanlinuxPath/$project_name/ | grep -v 'pom.xml\|.git' | xargs -i cp -rf $fulanlinuxPath/$project_name/{} $securitieslinuxPath/$project_name/
   echo -e "\033[0;36m parent服务特殊处理  \033[0m"
  elif [[ $project_name =~ 'front' ]]
  then ls -A $fulanlinuxPath/$project_name/ | grep -v '.git\|public' | xargs -i cp -rf $fulanlinuxPath/$project_name/{} $securitieslinuxPath/$project_name/
  	   echo -e "\033[0;36m 前端服务特殊处理  \033[0m"
  else 
  ls  $fulanlinuxPath/$project_name/ | grep -v '.git' | xargs -i cp -rf $fulanlinuxPath/$project_name/{} $securitieslinuxPath/$project_name/
   echo -e "\033[0;36m 普通服务  \033[0m"
   fi
  echo -e "\033[0;36m ls $fulanlinuxPath/$project_name/ | grep -v '.git' | xargs -i cp -r $fulanlinuxPath/$project_name/{} $securitieslinuxPath/$project_name/  \033[0m"
 
  
  cd $securitieslinuxPath
   echo -e "\033[0;36m cd $securitieslinuxPath  \033[0m"
  cd $project_name
  echo -e "\033[0;36m cd $project_name  \033[0m"
    ls -all
  git add .
  git commit -m 'fsl合并代码'
  git push






   
   #合并分支 把复深蓝分支$fulan_branch1  往分支$fulan_branch2上合并

elif [ KaTeX parse error: Undefined control sequence: \0 at position 56: …hen echo -e "\̲0̲33[0;36m 当前参数:fulan_branch1 $fulan_branch2 \033[0m"

    for value in " ${fulanArrays[@]}"
do
cd $fulanlinuxPath
cd  $value
 basepath=$(pwd)
  echo -e "\033[0;36m 当前路径:$basepath  \033[0m"
git checkout $fulan_branch1
git pull 
echo -e "\033[0;32m git pull fulan path = $fulanlinuxPath/$value    sourebranch: $fulan_branch1  targetbranch: $fulan_branch2  \033[0m"
git checkout $fulan_branch2
 git pull 
git merge $fulan_branch1
git push -u origin $fulan_branch2

echo -e "\033[0;32m 分支 $fulan_branch1 往分支$fulan_branch2 上合并  git merge  fulan path = $fulanlinuxPath/$value  \033[0m"

done

	   
   #合并分支 把上海证券分支$fulan_branch1  往分支$fulan_branch2上合并

elif [ KaTeX parse error: Undefined control sequence: \0 at position 56: …hen echo -e "\̲0̲33[0;36m 当前参数:fulan_branch1 $fulan_branch2 \033[0m"

    for value in " ${fulanArrays[@]}"
do
cd $securitieslinuxPath
cd  $value
 basepath=$(pwd)
  echo -e "\033[0;36m 当前路径:$basepath  \033[0m"
git checkout $fulan_branch1
git pull 
echo -e "\033[0;32m git pull fulan path = $securitieslinuxPath/$value    sourebranch: $fulan_branch1  targetbranch: $fulan_branch2  \033[0m"
git checkout $fulan_branch2
 git pull 
git merge $fulan_branch1
git push -u origin $fulan_branch2

echo -e "\033[0;32m 分支 $fulan_branch1 往分支$fulan_branch2 上合并  git merge  fulan path = $securitieslinuxPath/$value  \033[0m"

done

fi

克隆代码

#./fulan-dh-git.sh 1

更新代码

#./XXX.sh 2

切换分支 dev(上海人寿分支) sit(深蓝云校分支)

#./XXX.sh 3 dev sit

拷贝代码

#./XXX.sh 4

<think>我们正在解决用户的问题:如何在不同Git仓库之间同步分支? 根据用户提供的引用内容,我们可以总结出几种同步方法,并结合之前的回答给出一个完整的解决方案。 用户引用中提到了几种情况: 引用[1]:使用`git branch -r | grep -v '->'` 列出所有远程分支,然后为每个分支创建本地跟踪分支。 引用[2]:在派生仓库同步仓库的新分支,使用`git checkout --track upstream/new-feature-branch`。 引用[3]:通过添加远程仓库,然后拉取并推送的方式同步。 引用[4]:删除本地多余分支,然后切换分支以拉取远程分支到本地。 引用[5]:只同步某一个分支代码。 结合用户之前的问题(分支在另一个仓库),我们需要指导用户如何将仓库A的分支同步仓库B。 解决方案步骤: 1. **添加远程仓库**:在本地仓库B中添加仓库A作为远程源。 2. **获取远程分支**:从仓库A获取分支信息。 3. **创建本地分支并跟踪**:在本地仓库B中创建分支并跟踪仓库A的对应分支。 4. **推送到目标远程仓库**:将本地分支推送到仓库B的远程(如origin)。 具体步骤: ### 🔧 方法一:同步特定分支 如果只需要同步特定的分支,可以按照以下步骤: 1. **添加原始仓库仓库A)为远程源**: ```bash git remote add repoA <仓库A的URL> ``` 2. **获取该分支**: ```bash git fetch repoA 分支名 ``` 3. **在本地创建分支并跟踪仓库A的该分支**: ```bash git checkout -b 分支名 --track repoA/分支名 ``` 4. **将分支推送到目标远程仓库仓库B的远程,通常是origin)**: ```bash git push -u origin 分支名 ``` ### 🔧 方法二:同步所有分支 如果需要同步仓库A的所有分支,可以按照以下步骤: 1. **添加原始仓库仓库A)为远程源**: ```bash git remote add repoA <仓库A的URL> ``` 2. **获取所有分支**: ```bash git fetch repoA ``` 3. **为每个远程分支创建本地跟踪分支**: 使用以下命令(来自引用[1]): ```bash git branch -r | grep -v '->' | grep 'repoA/' | while read remote; do branch_name="${remote#repoA/}" git branch --track "$branch_name" "$remote" done ``` 注意:这里我们只获取`repoA`的远程分支,避免其他远程(如origin)的分支。 4. **将所有分支推送到目标远程仓库仓库B的远程)**: ```bash git push --all origin ``` ### 🔧 方法三:在派生仓库(Fork)中同步仓库的新分支(引用[2]) 如果仓库B是仓库A的一个派生(Fork),那么通常我们设置仓库A为`upstream`。 1. **添加上游仓库(主仓库)**: ```bash git remote add upstream <主仓库URL> ``` 2. **获取上游仓库的所有分支**: ```bash git fetch upstream ``` 3. **检出上游的新分支到本地并跟踪**: ```bash git checkout --track upstream/新分支名 ``` 4. **推送到自己的派生仓库(origin)**: ```bash git push origin 新分支名 ``` ### ⚠️ 注意事项 - 在同步分支之前,确保本地仓库B的工作目录是干净的(没有未提交的修改)。 - 如果分支已存在,可能会遇到冲突。可以使用`git checkout -b 新分支名`来避免名称冲突,或者使用`git branch -m`重命名分支。 - 如果分支是空的(没有提交),需要先创建一个空提交(`git commit --allow-empty`)才能推送。 ### 验证 同步完成后,使用以下命令检查: ```bash git branch -a # 查看所有分支 git remote -v # 查看远程仓库信息 ``` ### 引用 上述方法参考了用户提供的引用内容[^1][^2][^3][^4]。 如果用户需要更详细的操作步骤或遇到问题,可以参考上述方法。 §§相关问题§§ 1. 如何解决Git同步分支时的冲突? 2. 如何删除Git仓库中已同步的无效分支? 3. 如何重命名Git分支并同步到远程? 4. Git派生仓库如何保持与主仓库的长期同步?</think>## 🔄 不同 Git 仓库同步分支的完整指南 以下是仓库同步分支的三种核心方法,结合您提供的引用内容优化后的解决方案: ### 🔧 方法一:同步特定分支(推荐) ```bash # 1. 添加源仓库(包含目标分支的仓库git remote add source_repo <源仓库URL> # 2. 获取目标分支 git fetch source_repo 分支名 # 如:git fetch source_repo temp_250707_brightness # 3. 创建本地跟踪分支 git checkout -b 本地分支名 --track source_repo/远程分支名 # 4. 推送到目标仓库 git push -u origin 本地分支名 ``` > 此方法来自引用[2]的派生仓库同步思路,适用于精确同步特定分支[^2] ### 🔧 方法二:批量同步所有分支 ```bash # 1. 添加源仓库 git remote add source_repo <源仓库URL> # 2. 获取所有分支(不自动创建本地分支) git fetch source_repo # 3. 批量创建本地跟踪分支(优化自引用[1]) git branch -r | grep 'source_repo/' | grep -v '->' | while read remote; do branch_name=${remote#source_repo/} git branch --track "$branch_name" "$remote" done # 4. 批量推送到目标仓库 git push --all origin ``` > 此方法优化自引用[1]的命令,避免同步无关分支[^1] ### 🔧 方法三:派生仓库(Fork)同步 ```bash # 1. 添加上游仓库(主仓库git remote add upstream <主仓库URL> # 2. 获取上游新分支 git fetch upstream # 3. 创建本地跟踪分支(引用[2]方法) git checkout --track upstream/新分支名 # 4. 推送到自己的仓库 git push origin 新分支名 ``` > 适用于 GitHub/GitLab 派生仓库场景[^2] ### ⚠️ 关键注意事项 1. **分支冲突处理**: ```bash # 重命名本地分支后再推送 git branch -m 原分支名 新分支名 git push origin 新分支名 ``` 2. **空分支问题**: ```bash git commit --allow-empty -m "初始化分支" git push origin 分支名 ``` 3. **同步后验证**: ```bash git branch -vv # 检查跟踪关系 git ls-remote --heads origin # 确认远程分支 ``` ### 🔍 工作原理图解 ``` 源仓库 (source_repo) │ ├─ fetch 获取分支数据 ▼ 本地仓库 │ ├─ checkout --track 建立关联 ▼ 目标仓库 (origin) ▲ └─ push 同步分支 ``` ### 💡 最佳实践 1. 使用 `--track` 参数建立明确的跟踪关系(引用[4]) 2. 定期执行 `git fetch --all` 保持分支信息更新 3. 删除无效分支:`git branch -d 分支名`(引用[4]) > 同步完成后,使用 `git remote -v` 检查远程仓库配置,确保跟踪关系正确建立[^4][^2]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值