1、创建或切换到目标分支
如果需要在新分支操作,首先创建新分支:
git checkout -b <branch-name> # 创建新分支
如果在已有分支上操作,切换到目标分支:
git checkout master-test # 切换到目标分支
2、关联 A 项目远程仓库地址
添加 A 项目的远程仓库地址,起个别名(如 a-project):
git remote add a-project http://xxxxx.git
3、检查是否关联成功
使用以下命令查看已关联的远程仓库:
git remote # 会显示关联的仓库别名
4、 拉取 A 项目指定分支代码
拉取 A 项目中的 test
分支代码:
git pull a-project test # 拉取可能会失败
如果出现错误提示:
fatal: refusing to merge unrelated histories
需要添加 --allow-unrelated-histories
选项:
git pull a-project test --allow-unrelated-histories # 允许合并不相关的历史
5、验证合并结果
合并完成后,可以使用 git log
命令查看提交记录,确认 A 项目 test
分支的代码已经成功合并到 B 项目的 master-test
分支。
git log # 查看提交记录