jgit查询远程仓库_java 使用jgit 操作 git
如果你想在一个 Java 程序中使用 Git ,有一个功能齐全的 Git 库,那就是 JGit 。 JGit 是一个用 Java 写成的功能相对健全的 Git 的实现,它在 Java 社区中被广泛使用。 JGit 项目由 Eclipse 维护,它的主页在 http://www.eclipse.org/jgit 。
非常好的例子:https://github.com/centic9/jgit-cookbook
在本地文件夹建立起与远程仓库的连接
根据主干master新建分支并同步到远程
提交commit文件到远程
从远程拉去代码到本地文件夹
public class GitUtilClass {
public static String localRepoPath = "D:/repo";
public static String localRepoGitConfig = "D:/repo/.git";
public static String remoteRepoURI = "git@gitlab.com:wilson/test.git";
public static String localCodeDir = "D:/platplat";
/**
* 新建一个分支并同步到远程仓库
* @param branchName
* @throws IOException
* @throws GitAPIException
*/
public static String newBranch(String branchName){
String newBranchIndex = "refs/heads/"+branchName;
String gitPathURI = "";
Git git;
try {
//检查新建的分支是否已经存在,如果存在则将已存在的分支强制删除并新建一个分支
List refs = git.branchList().call();
for (Ref ref : refs) {
if (ref.getName().equals(newBranchIndex)) {
System.out.println("Removing branch before");
git.branchDelete()
.setBranchNames(branchName)
.setForce(true)
.call();
break;
}
}
//新建分支
Ref ref = git.branchCreate().setName(branchName).call();
//推送到远程
git.push().add(ref).call();
gitPathURI = remoteRepoURI + " " + "feature/" + branchName;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (GitAPIException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return gitPathURI;
}
public static void commitFiles() throws IOException, GitAPIException{
String filePath = "";
Git git = Git.open( new File(localRepoGitConfig) );
//创建用户文件的过程
File myfile = new File(filePath);
myfile.createNewFile();
git.add().addFilepattern("pets").call();
//提交
git.commit().setMessage("Added pets").call();
//推送到远程
git.push().call();
}
public static boolean pullBranchToLocal(String cloneURL){
boolean resultFlag = false;
String[] splitURL = cloneURL.split(" ");
String branchName = splitURL[1];
String fileDir = localCodeDir+"/"+branchName;
//检查目标文件夹是否存在
File file = new File(fileDir);
if(file.exists()){
deleteFolder(file);
}
Git git;
try {
git = Git.open( new File(localRepoGitConfig) );
git.cloneRepository().setURI(cloneURL).setDirectory(file).call();
resultFlag = true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (GitAPIException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return resultFlag;
}
public static void deleteFolder(File file){
if(file.isFile() || file.list().length==0){
file.delete();
}else{
File[] files = file.listFiles();
for(int i=0;i
deleteFolder(files[i]);
files[i].delete();
}
}
}
public static void setupRepo() throws GitAPIException{
//建立与远程仓库的联系,仅需要执行一次
Git git = Git.cloneRepository()
.setURI(remoteRepoURI)
.setDirectory(new File(localRepoPath))
.call();
}
}
本文由 创作,采用 知识共享署名4.0 国际许可协议进行许可。本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。最后编辑时间为:
2020/05/17 02:32
jgit查询远程仓库_java 使用jgit 操作 git相关推荐
- jgit查询远程仓库_JAVA 使用jgit管理git仓库
JAVA 使用jgit管理git仓库 最近设计基于gitops新的CICD方案,需要通过java读写git仓库,这里简单记录下. JGit是一款pure java的软件包,可以读写git仓库,下面介绍 ...
- jgit查询远程仓库_通过 JGit 访问 Git 仓库
一个Git库仓在 JGit里面表现为Repository类,该类可以看作是这个库的句柄.通过Repository类实例,你可以创建JGit命令以及可以控制配置.参数等. 获取Git库仓的引用有很多种方 ...
- git 远程仓库版本的回退以及git reset 几种常用方式记录
由于 github push 了两个比较潦草的commit, 自己很不满意,又不想重新开vpn进行上传,所以找了一下相关的教程. 最后研究了一下,原理为先在本地还原到你想要的commit,然后强制pu ...
- Git 常用操作(6)- 推送到远程仓库(git push)删除远程分支(git push origin --delete)
1. git remote add--添加远程仓库 在GitHub 上创建的仓库路径为 "git@github.com:用户名/git-tutorial.git".现在我们用git ...
- Git 高频命令、版本回退、分支操作、文件修改删除、撤销、标签、远程仓库推送、拉取
1. 高频命令 git add 将工作文件修改提交到本地暂存区. git add . 将所有修改过的工作文件提交暂存区(常用). git commit -m "XXX" 为本次修改 ...
- [超详细] Git 远程仓库及回滚日志操作
一.远程仓库基本操作 1.拿到仓库地址(以GitEE为例): 2.初始化Git: 使用命令:git init 在准备存放的文件夹里使用命令git init进行初始化,也就是把这个文件夹变成git专属文 ...
- Git系列(四)、在IDEA操作Git本地仓库与连接远程Git仓库(推送、合并、拉取、克隆操作)
在IDEA操作Git本地仓库与连接远程Git仓库 1.首先设置idea中绑定本地Git的可执行路径 2.随便建个项目,将项目初始化为Git管理的本地仓库 3.按命令操作顺序将项目添加并提交到本地仓库中 ...
- Git与远程仓库的连接操作
文章目录 版本控制Git 1.1 Git介绍 1.3 cmd窗口操作 1.3 IDEA创建内置Git 版本控制Git 1.1 Git介绍 为什么要用Git? Git可以对代码进行备份,不至于丢掉: 对 ...
- 代码管理平台介绍、安装svn、客户端使用svn、远程仓库、分支管理、标签管理、git别名、搭建git服务、安装gitlab、简单使用gitlab、备份和恢复
22.1 代码管理平台介绍 现在的开发工作都是由团队合作来完成开发,通常都是团队中的每个人或者每几个人完成一个模块的开发,最后再将模块拼凑起来,形成一个完整的项目,这就涉及到了协同开发.在各个模块的开 ...
最新文章
- anguarjs 图片预览_Text to Image Converter(文字转图片软件)下载-Text to Image Converter(文字转图片软件)v1.5绿色版下载...
- Windows 7平台安装Oracle Client 10g版本时需要做的一点变更
- Doxygen从零学起———安装和配置
- python对象分类
- 统计学习方法-李航(5)
- wifi情况下使用fiddler_fiddler常见的应用场景
- 文件管理英文html,Directory Opus
- AngularJS 学习笔记 (六) 基本概念和用法 之 Service与Provider
- 用三张图宏观把握数据库
- Vue脚手架组件开发常见问题
- MASM8.0 下载安装方法
- 解读国密非对称加密算法SM2
- 重装服务器系统鼠标键盘用不了,win7重装系统后鼠标键盘不能用怎么办
- Ardunio开发实例-AM2320温湿度传感器
- 绿色软件联盟:杀毒软件已经进入空前弱智与混沌状态
- 全自动与半自动手表的区别_全自动和半自动机械表的区别?
- linux中cpu_to_be32,Linux cpufreq framework(2)
- vue项目中实现汉字转拼音缩写
- Python之禅及其翻译
- PHP 运行方式详解