当前位置: 首页 > news >正文

Jgit的使用

Jgit的使用

文章目录

  • Jgit的使用
  • 一,git操作的对应代码
  • 1.1 查看操作
      • 1.1.1 打开仓库
      • 1.1.3 获取状态信息
    • 1.2 添加操作
      • 1.2.1 初始化本地仓库
      • 1.2.2 创建一个新文件并写入内容
      • 1.2.3 添加指定(所有)文件到暂存区
      • 1.2.4 提交操作
      • 1.2.5 连接并推送到远程仓库

当需要对系统中某些页面管理的文件做版本管理时,使用git作为其基本组件。

此时需要在页面上做一些按钮,操作时会执行git指令。

使用java作为开发语言时,需要引用jgit依赖

例如

<dependency><groupId>org.eclipse.jgit</groupId><artifactId>org.eclipse.jgit</artifactId><version>6.5.0.202303070854-r</version>
</dependency>

一,git操作的对应代码

1.1 查看操作

1.1.1 打开仓库

Git git = Git.open(new File(repoPath));

举例:

String repositoryPath = "E:\Workspace\GitHab\test-git-demo";
Repository repository = new FileRepositoryBuilder().setGitDir(new File(repositoryPath, ".git")).build();
Git git = new Git(repository);// 或Git git = Git.open(new File(repositoryPath));

1.1.3 获取状态信息

Status status = git.status().call();

1.2 添加操作

1.2.1 初始化本地仓库

// 执行代码前不存在new-git-repository-demo目录
String repositoryPath = "E:\Workspace\GitHab\new-git-repository-demo";
Git.init().setDirectory(repositoryPath).call();Repository repository = Git.init().setGitDir(repoDir).call().getRepository()

1.2.2 创建一个新文件并写入内容

File file = new File(repositoryPath, "xxx.txt");
FileWriter writer = new FileWriter(file);
writer.write("Hello, JGit!\n");
writer.close();

1.2.3 添加指定(所有)文件到暂存区

// 不需要将repositoryPath目录写上,从该目录下开始即可
git.add().addFilepattern("xxx.txt").call();// 指定所有文件
git.add().addFilepattern(".").call();

1.2.4 提交操作

// 提交更改
git.commit().setMessage("Commit message for add and modified file xxx.txt").call();

1.2.5 连接并推送到远程仓库

String remoteRepoUrl = "https://github.com/"
String userName = "git账号";
String password = "git账号密码";
// 连接到远程仓库
git.remoteAdd().setName("origin").setUri(new java.net.URI(remoteRepoUrl)).call();CredentialsProvider credentialsProvider = new UsernamePasswordCredentialsProvider(userName, password);
git.push().setCredentialsProvider(credentialsProvider).call();

到这里,虽然jgit能够做很多git操作,但如果需要直接在服务器创建远程仓库,单靠jgit还是不够的,此时就需要gitlab4j-api。

gitlab4j-api提供了更多的gitlab中API的调用方法。

http://www.lryc.cn/news/455605.html

相关文章:

  • SQL Server—约束和主键外键详解
  • 信息学奥赛复赛复习14-CSP-J2021-03网络连接-字符串处理、数据类型溢出、数据结构Map、find函数、substr函数
  • Allegro如何合并同名网络铜皮操作指导
  • 【探测器】线阵相机中的 TDI 技术
  • k8s 之安装metrics-server
  • java学习-idea编辑器基础使用设置
  • PDSCH(物理下行共享信道)简介
  • hutool bug
  • 69.x的平方根 367.完全有效的平方数
  • Android Automotive(一)
  • 命令设计模式
  • 探索智能新境界:最好用的AI工具盘点
  • 【Redis】持久化(下)-- AOF
  • 用Arduino单片机制作一个简单的音乐播放器
  • 软件工程相关
  • 速盾:游戏加速下载可以用cdn吗?
  • 每日新闻掌握【2024年9月25日 星期三】
  • 8. Bug 与 Error
  • 论文 | Model-tuning Via Prompts Makes NLP Models Adversarially Robust
  • 828华为云征文|华为云Flexus云服务器X实例部署 即时通讯IM聊天交友软件——高性能服务器实现120W并发连接
  • 超好用的element的el-pagination分页组件二次封装-附源码及讲解
  • 【AIGC】通过OpenAi Canvas修改论文(附40条论文优化指令)
  • Kubernetes Pod详解
  • Vue2电商项目(七)、订单与支付
  • 你知道U盘怎么加密吗?
  • 【软件教程OBS下载使用】一篇文章教会你如何下载安装使用OBS-Studio
  • 鸿蒙next开发第一课03.ArkTs语法介绍-案例
  • HTML网页制作——设计系学生静态HTML网页设计作品
  • 智能翻译新纪元:4款英汉互译在线工具解析
  • Cisco Meraki平台中国区注册