本文紧接【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);// 添加分支 devSystem.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
命令。
附
- [【JGit】简述及学习资料整理]([JGit ]简述及学习资料整理-CSDN博客)
- 【Gitea】Java 使用 JGit 创建 Git 代码仓库
- Win 下 Docker 安装 Gitea 实践:windows docker desktop部署gitea