天天看點

git-p4 多個分支與版本合并 merge

git-p4

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

多個分支

假設已經用 p4 sync 同步了 2 個 p4 分支:

client client type p4 遠端位址 本地目錄
tl_fananchong_vm1 stream client //Torchlight/MainLineWithUGS/backend/ /home/fananchong/MainLineWithUGS
tl_fananchong_vmweek stream 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 分支
    [email protected]:~/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 <[email protected]>
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 <[email protected]>
# 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 type view 沖突點
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 名等問題

以上

繼續閱讀