git多个项目合并到一个项目中

satuo20 1月前 ⋅ 56 阅读
  • 创建新项目
mkdir kp-exampro
cd kp-exampro
git init
  • 创建远程分支
git remote add origin_merge_exammark_engine http://gogs.local.yjzhixue.com:8081/kp-plat/lazyjee-exammark-engine.git
git remote add origin_merge_exam_scan http://gogs.local.yjzhixue.com:8081/kp-plat/lazyjee-exam-scan.git
git remote add origin_merge_exam_result http://gogs.local.yjzhixue.com:8081/kp-plat/lazyjee-exam-result.git
git remote add origin_merge_exam_manager http://gogs.local.yjzhixue.com:8081/kp-plat/lazyjee-exammanager.git
git remote add origin_merge_exam_card http://gogs.local.yjzhixue.com:8081/kp-plat/lazyjee-card-mng.git
  • 删除分支(如果有问题)
git remote remove origin_merge_exam_result
  • 代码拉取
git fetch origin_merge_exammark_engine feature_5.3.7
git fetch origin_merge_exam_scan feature_5.3.7
git fetch origin_merge_exam_result feature_5.3.6
git fetch origin_merge_exam_manager feature_5.3.7
git fetch origin_merge_exam_card feature_5.3.7
  • 合并
git merge origin_merge_exammark_engine/feature_5.3.7 --allow-unrelated-histories
git merge origin_merge_exam_scan/feature_5.3.7 --allow-unrelated-histories
git merge origin_merge_exam_result/feature_5.3.6 --allow-unrelated-histories
git merge origin_merge_exam_manager/feature_5.3.7 --allow-unrelated-histories
git merge origin_merge_exam_card/feature_5.3.7 --allow-unrelated-histories
  • 移动文件并提交日志
mkdir kp-exam-mark-engine
将项目移动到kp-exam-mark-engine目录下
# 生成一条commit日志
git add . && git commit -m "merge origin_merge_exammark_engine and mv to kp-exam-mark-engine"

mkdir kp-exam-scan
将项目移动到kp-exam-scan目录下
# 生成一条commit日志
git add . && git commit -m "merge origin_merge_exam_scan and mv to kp-exam-scan"

mkdir kp-exam-result
将项目移动到kp-exam-result目录下
# 生成一条commit日志
git add . && git commit -m "merge origin_merge_exam_result and mv to kp-exam-result"


mkdir kp-exam-manager
将项目移动到kp-exam-manager目录下
# 生成一条commit日志
git add . && git commit -m "merge origin_merge_exam_manager and mv to kp-exam-manager"

mkdir kp-exam-card
将项目移动到kp-exam-manager目录下
# 生成一条commit日志
git add . && git commit -m "merge origin_merge_exam_card and mv to kp-exam-card"
  • 修改项目名称,实现名称对换(登录gogs,修改项目名称即可)

  • 代码合并实现(将两个一样功能,不同名称的git项目合并)

package com.yjzhixue.clean.utils;

import cn.hutool.core.io.FileUtil;
import org.apache.commons.codec.digest.DigestUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

public class CodeFileUtil {


    private static final String FILE_TYPE = ".java"; // 指定要覆盖的文件类型后缀,可按需更改

    public static void main(String[] args) throws Exception {
        String sourceFolderPath = "/Users/xiaodx/IdeaProjects/kp-exam-old"; // 源目录路径,需替换为实际路径
        String targetFolderPath = "/Users/xiaodx/IdeaProjects/kp-exam"; // 目标目录路径,需替换为实际路径
        File sourceFolder = FileUtil.file(sourceFolderPath);
        File targetFolder = FileUtil.file(targetFolderPath);

        if (!FileUtil.exist(sourceFolder) ||!FileUtil.isDirectory(sourceFolder)) {
            System.err.println("源文件夹不存在或不是有效的目录");
            return;
        }

//        if (!FileUtil.exist(targetFolder)) {
//            FileUtil.mkdir(targetFolder);
//        }

        overwriteFolderWithFileType(sourceFolder, targetFolder);
    }

    public static void overwriteFolderWithFileType(File sourceFolder, File targetFolder) throws Exception {
        List<File> fileList = FileUtil.loopFiles(sourceFolder);
        for (File file : fileList) {
//            System.err.println(file);
            if (file.isFile() && (file.getName().toLowerCase().endsWith(FILE_TYPE.toLowerCase())
                    || file.getName().toLowerCase().endsWith("pom.xml")
                    || file.getName().toLowerCase().endsWith("Dockerfile")
                    || file.getName().toLowerCase().endsWith("logback-spring.xml")
                    || file.getName().toLowerCase().endsWith("bootstrap.yml"))) {
                String sourceMd5 = md5(file);
                String absolutePath = file.getAbsolutePath();
                String refPath = absolutePath.replace("/Users/xiaodx/IdeaProjects/kp-exam-old","");
                File targetFile = FileUtil.file(targetFolder, refPath);
                if(!targetFile.exists()) {
                    targetFile.getParentFile().createNewFile();
                    targetFile.createNewFile();
                }
                String targetMd5 = md5(targetFile);
                if(!sourceMd5.equals(targetMd5)) {
                    FileUtil.copy(file, targetFile, true);
                    System.err.println("覆盖文件:"+targetFile.getAbsoluteFile());
                }
            }
        }
    }
    public static String md5(File file) throws IOException {
        try (FileInputStream fis = new FileInputStream(file)) {
            return DigestUtils.md5Hex(fis);
        }
    }
}


全部评论: 0

    我有话说: