github比较两个repo是否一致

絮叨的背景
  • 之前,在个人的目录下创建了一个repo,后来想迁移到小组的目录下
  • 使用的gitlab做代码管理,使用的是gitlab的项目导入功能(在gitlab中repo叫project)
# 1.导出项目
进入原始的项目 → settings → export project

# 2.导入项目
New project → Import project →  Gitlab export
  • 没有使用过这样的迁移方式,担心迁移后的项目缺失了某些数据
  • 因此,想要比较两个项目每个分支的内容是否一致
比较两个repo是否一致
  • 我选择将原始repo(叫做repo_a)作为比较的base,先将新的repo(叫做repo_b)添加到原始repo中
git remote add -f repo_b path/to/repo_b.git   # path就是repo_b的git clone的url
git remote update
  • 按照分支进行比较
# 1.展示所有的分支
git branch -a
# 比较两个repo的同一分支,前者为repo_a的分支,后者为repo_b的分支
git diff remotes/origin/dev-1.0.25 remotes/olap-platform/dev-1.0.25
  • 完成比较后,删除repo_b
git remote rm repo_b
分支太多,懒得拼凑命令
  • 分支太多,懒得拼凑命令,写个java程序,拼凑出所有的diff命令(写得很粗糙)
// 执行shell脚本
String command = "git branch -a";
Process pcs = Runtime.getRuntime().exec(command, null, new File("E:\\JavaProject\\druid-platform"));

// 获取shell返回流
BufferedInputStream in = new BufferedInputStream(pcs.getInputStream());
// 字符流转换字节流
BufferedReader br = new BufferedReader(new InputStreamReader(in));
// 这里也可以输出文本日志

String lineStr = "";
Set<String> branches = new HashSet<>();
while ((lineStr = br.readLine()) != null) {
    branches.add(lineStr.trim());
}
System.out.println(command + ": " + branches);

// 遍历值,比较remotes/olap-platform每个分支与remotes/origin每个分支是否一致
int count = 0;
for (String branch : branches) {
    if (branch.startsWith("remotes/olap-platform")) {
        System.out.println("========================================================");
        String[] splits=branch.split("/");
        String sourceBranch = "remotes/origin/" + splits[2];
        System.out.println(branch + ": " + sourceBranch);
        // 执行命名比较分支的异同
        command = "git diff " + sourceBranch + " " + branch;
        System.out.println("command: " + command);
        count++;
    }
}

System.out.println("共有" + count + "个分支");

// 关闭流
br.close();
in.close();

参考链接:了解两个存储库之间的区别

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值