【JGit】分支管理实践

本文紧接【JGit】简述及学习资料整理

以下梳理了使用 JGit 进行 Git 操作的实践

JGit实践

主函数

public static void main(String[] args) throws Exception {
   
        String localDir = "D:\\tmp\\git-test\\";

        String gitUrl = "http://192.168.181.1:3000/root/git-test-by-code.git";

        String username = "root";
        String password = "123456";
        // 创建认证信息
        CredentialsProvider credentialsProvider = new UsernamePasswordCredentialsProvider(username, password);

        // 初始化 git 仓库
        Git git = Git.init().setDirectory(new File(localDir)).call();


        // Pull Test
        // pullTest(gitUrl, credentialsProvider, git);

        // ==== 分支管理
        // branchManage(git);

        // ==== 创建分支,并将其推送到远程仓库
		newBranchAndPush(credentialsProvider, git);
		// ==== 删除远程分支
		//deleteRemoteBranch(credentialsProvider, git);

        // 关闭 git 命令
        git.close();
    }

拉取代码

private static void pullTest(String gitUrl, CredentialsProvider credentialsProvider, Git git)
            throws GitAPIException, URISyntaxException, WrongRepositoryStateException, InvalidConfigurationException,
            InvalidRemoteException, CanceledException, RefNotFoundException, RefNotAdvertisedException, NoHeadException,
            TransportException {
   
        // 添加远程仓库信息
        git.remoteAdd()
                .setName("origin")
                .setUri(new URIish(gitUrl))
                .call();

        // 代码拉取
        PullResult pullResult = git.pull()
                .setCredentialsProvider(credentialsProvider)
                .call();

        log.info(">>> " + pullResult);
        if (pullResult.isSuccessful()) {
   
            log.info(">>> pull Result is Success");
        } else {
   
            log.error(">>> pull Result is Failes ");
        }
    }

分支管理

private static void branchManage(Git git) throws Exception {
   
        // 列出所有分支
        branchList(git);

        // 添加分支 dev
        System.err.println("<<< 添加分支");
        git.branchCreate().setName("dev").call();
        branchList(git);

        // 修改分支名
        System.err.println("<<< 修改分支");
        git.branchRename().setOldName("dev").setNewName("dev-new").call();
        branchList(git);

        // 删除分支
        System.err.println("<<< 删除分支");
        git.branchDelete().setBranchNames("dev-new").call();
        branchList(git);
    }

private static void branchList(Git git) throws Exception {
   
    // 获取默认分支
        String currentBranch = git.getRepository().getBranch();
        List<Ref> call = git.branchList().call();
        for (Ref ref : call) {
   
            boolean symbolic = ref.isSymbolic();
            boolean peeled = ref.isPeeled();
            String name = ref.getName();
            if(currentBranch.equals(name.substring(name.lastIndexOf('/') + 1))){
   
                name = "* \t"+ name;
            }else{
   
                name = "\t" + name;
            }
            System.out.println(">>> \t"+ name + "  " + ref.getObjectId().getName() + "  ,symbolic:" + symbolic + ", peeled: " + peeled);
        }
    }

创建新分支并推送到远程服务器

 private static void newBranchAndPush(CredentialsProvider credentialsProvider, Git git) throws Exception{
   
     // 创建 dev 分支
        Ref branchRef = git.branchCreate().setName("dev").call();

        // 推送分支到远程仓库
        Iterable<PushResult> results = git.push()
                .setCredentialsProvider(credentialsProvider)
                .setRemote("origin")
                .add(branchRef)
                .call();

        // 处理推送结果
        for (PushResult result : results) {
   
            for (RemoteRefUpdate update : result.getRemoteUpdates()) {
   
                System.out.println("Status: " + update.getStatus());
            }
        }
}

推送结果展示

在这里插入图片描述

删除远程分支

private static void deleteRemoteBranch(CredentialsProvider credentialsProvider, Git git) throws GitAPIException {
   
        String deleteBranch = "dev";
        RefSpec refSpec = new RefSpec()
                .setSource(null)
                .setDestination("refs/heads/" + deleteBranch);
         
        Iterable<PushResult> results = git.push()
                .setCredentialsProvider(credentialsProvider)
                .setRemote("origin")
                .setRefSpecs(refSpec)
                .call();

        // 处理推送结果
        for (PushResult result : results) {
   
            for (RemoteRefUpdate update : result.getRemoteUpdates()) {
   
                System.out.println("Status: " + update.getStatus());
            }
        }
    }

以上代码相当关于执行了 git push origin --delete dev 命令。

相关推荐

  1. 【Flutter 面试题】 JIT 与 AOT分别是什么?

    2024-02-21 07:00:02       11 阅读
  2. git分支-分支管理

    2024-02-21 07:00:02       15 阅读
  3. git分支-分支管理

    2024-02-21 07:00:02       13 阅读
  4. Jgit Packfile is truncated解决方案

    2024-02-21 07:00:02       29 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-21 07:00:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-21 07:00:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-21 07:00:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-21 07:00:02       18 阅读

热门阅读

  1. 基于opencv的SIFT特征提取

    2024-02-21 07:00:02       28 阅读
  2. NPM运行保存问题解决

    2024-02-21 07:00:02       27 阅读
  3. 整型数组按个位值排序/最低位排序(C语言)

    2024-02-21 07:00:02       33 阅读
  4. k8s集群5个故障案例分析

    2024-02-21 07:00:02       32 阅读
  5. 【npm】常见错误

    2024-02-21 07:00:02       28 阅读
  6. 分布式场景怎么Join | 京东云技术团队

    2024-02-21 07:00:02       29 阅读