git-p4 多个分支与版本合并 merge

11 篇文章 0 订阅

git-p4

git-p4 基本操作,见 https://blog.csdn.net/u013272009/article/details/120166796

多个分支

假设已经用 p4 sync 同步了 2 个 p4 分支:

clientclient typep4 远程地址本地目录
tl_fananchong_vm1stream client//Torchlight/MainLineWithUGS/backend//home/fananchong/MainLineWithUGS
tl_fananchong_vmweekstream client//Torchlight/weeklineWithUGS/backend//home/fananchong/weeklineWithUGS

下面介绍,如何使用 git-p4 来做把 MainLineWithUGS 分支的代码合并到 weeklineWithUGS

制作多个分支的 git 仓库

举例说明:

  1. 把 git 仓库建在 /home/fananchong/git2/ugs/backend

    mkdir -p /home/fananchong/git2/ugs/backend
    pushd /home/fananchong/git2/ugs/backend
    git init
    popd
    
  2. 把 2 个 p4 分支同步到 git 仓库

    pushd /home/fananchong/git2/ugs/backend
    git p4 sync --branch=MainLineWithUGS //Torchlight/MainLineWithUGS/backend/...
    git checkout -b MainLineWithUGS p4/MainLineWithUGS
    git p4 sync --branch=weeklineWithUGS //Torchlight/weeklineWithUGS/backend/...
    git checkout -b weeklineWithUGS p4/weeklineWithUGS
    popd
    
  3. 检查 git 分支

    fananchong@ubuntu-vm:~/git2/ugs/backend$ git branch -a
    * MainLineWithUGS
      weeklineWithUGS
      remotes/p4/HEAD -> p4/MainLineWithUGS
      remotes/p4/MainLineWithUGS
      remotes/p4/weeklineWithUGS
    

至此, git 仓库对应 p4 的分支已经创建完毕

下面介绍如何多分支下,如何提交版本以及如何合并版本到其他分支

多分支下,提交版本

多分支下, git p4 rebase 命令不可以用。它没有提供参数来指定分支

不过看官方文档介绍:

This command does git p4 sync followed by git rebase to move local commits on top of updated p4 changes.

git p4 rebase 命令有 git p4 sync 与 git rebase 组成

实践下,确实可以完成多分支下,提交版本。

举例说明,以下命令可以完成提交:

# 本地操作,如
# git add xxx
# git commit -m "xxx"
# 下面演示如何提交
git p4 sync --branch=MainLineWithUGS
git rebase p4/MainLineWithUGS
P4CLIENT=tl_fananchong_vm1 git p4 submit --branch=MainLineWithUGS

如果遇到有代码冲突的,以下命令可以完成提交:

# 本地操作,如
# git add xxx
# git commit -m "xxx"
# 下面演示如何提交
git p4 sync --branch=MainLineWithUGS
git rebase p4/MainLineWithUGS
# 遇到冲突, IDE 中 git 插件提示有文件代码冲
# 手动解决冲突
# 然后重新提交改动的文件 xxx:
# git add xxx
# git commit -m "xxx"
git rebase --continue
P4CLIENT=tl_fananchong_vm1 git p4 submit --branch=MainLineWithUGS

合并版本到其他分支

举例说明(下面命令和命令的输出都显示,注意区分哪些是命令,哪些是输出):

# 先用 git log ,查看要合并的版本号。如 e55966e74515dfdb7760da8e9364e60f3f4ec109
git log
commit e55966e74515dfdb7760da8e9364e60f3f4ec109
Author: tl_backned_01 <tl_backned_01@p4d>
Date:   Sat Sep 25 17:36:16 2021 +0800

    test 1 2
    
    [git-p4: depot-paths = "//Torchlight/MainLineWithUGS/backend/": change = 585088]
# 切分支
git checkout weeklineWithUGS
# 合并版本,注意一定要加 -e ,目的是注释掉`[git-p4: depot-paths = "//Torchlight/MainLineWithUGS/backend/": change = 585088]`
git cherry-pick -e e55966e74515dfdb7760da8e9364e60f3f4ec109
test 1 2

[git-p4: depot-paths = "//Torchlight/MainLineWithUGS/backend/": change = 585088]
#
# It looks like you may be committing a cherry-pick.
# If this is not correct, please run
#       git update-ref -d CHERRY_PICK_HEAD
# and try again.


# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Author:    tl_backned_01 <tl_backned_01@p4d>
# Date:      Sat Sep 25 17:36:16 2021 +0800
#
# On branch weeklineWithUGS
# You are currently cherry-picking commit e55966e7.
#
# Changes to be committed:
#       modified:   test.txt
#
~                                                                                                                                                      
~                                                                                                                                                      
~                                                                                                                                                      
~                                                                                                                                                      
~                                                                                                                                                      
~                                                                                                                                                      
~                                                                                                                                                      
"~/git2/ugs/backend/.git/COMMIT_EDITMSG" 22L, 603B
# 下面提交,同上个章节内容,包括无冲突与有冲突 2 种处理
git p4 sync --branch=weeklineWithUGS
git rebase p4/weeklineWithUGS
P4CLIENT=tl_fananchong_vmweek git p4 submit --branch=weeklineWithUGS

整理下命令流程如下:

# 先用 git log ,查看要合并的版本号。如 e55966e74515dfdb7760da8e9364e60f3f4ec109
git log
# 切分支
git checkout weeklineWithUGS
# 合并版本,注意一定要加 -e ,目的是注释掉`[git-p4: depot-paths = "//Torchlight/MainLineWithUGS/backend/": change = 585088]`
git cherry-pick -e e55966e74515dfdb7760da8e9364e60f3f4ec109
# 下面提交,同上个章节内容,包括无冲突与有冲突 2 种处理
git p4 sync --branch=weeklineWithUGS
git rebase p4/weeklineWithUGS
P4CLIENT=tl_fananchong_vmweek git p4 submit --branch=weeklineWithUGS

重点说明:git cherry-pick -e <SHA>

如果不加 -e ,自动生成的提交注释种,有一行:[git-p4: depot-paths = "//Torchlight/MainLineWithUGS/backend/": change = 585088]
会导致,git p4 submit 去操作 MainLineWithUGS 分支,进而引发提交失败
这里 p4 client 类型设定上有个矛盾:

client typeview矛盾点
stream client本分支 veiw可提交代码,但是没法设置多个 view
no stream client无限制 view 个数可以设置多个分支 view ,但是没办法提交代码

我们合并好代码是要提交的,所以只能是 stream client 。所以需要手动加 -e 注释掉那行注释

PS. 如果有读者知道很好的处理方式,请留言告知

最佳实践

  1. 建议同时制作 master 版本 git 库和多分支版本 git 库
    • 平时大多数时间只要在 master 版本 git 库使用 git p4 rebase 与 git p4 submit
    • 和分支代码时,到多分支版本 git 库目录,按本文说的操作方式
  2. 本文提到的命令可 sh 脚本,封装成命令
    • 避免命令参数太长等问题
    • 避免敲错分支名、 client 名等问题

以上

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fananchong2

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值