在做一个项目中需要用到远程仓库,本来想使用svn的,但是svn的java api网上的资料很少,而且与git相比,svn显得笨重且不方便,因此放弃了svn转而使用git。java git api - jgit的资料还是比较多的,而且git的操作比svn更容易理解,所以毅然决然的在git的道路上越走越远。

如果你想在一个 Java 程序中使用 Git ,有一个功能齐全的 Git 库,那就是 JGit 。 JGit 是一个用 Java 写成的功能相对健全的 Git 的实现,它在 Java 社区中被广泛使用。 JGit 项目由 Eclipse 维护,它的主页在 http://www.eclipse.org/jgit 。

非常好的例子:https://github.com/centic9/jgit-cookbook

下面是我实现的代码,分别包含了如下的功能:

1、在本地文件夹建立起与远程仓库的连接

2、根据主干master新建分支并同步到远程

3、提交commit文件到远程

4、从远程拉去代码到本地文件夹

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<Ref> 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<files.length;i++){
deleteFolder(files[i]);
files[i].delete();
}
}
} public static void setupRepo() throws GitAPIException{
//建立与远程仓库的联系,仅需要执行一次
Git git = Git.cloneRepository().setURI(remoteRepoURI).setDirectory(new File(localRepoPath)).call();
} }

最新文章

  1. CSS基本知识3-CSS盒模型
  2. Silverlight ComboBox with TreeView
  3. iOS企业级开发初级课程-表视图(13集)
  4. 控制器与xib关联(用xib布局控制器)
  5. http相关概念在iOS中的使用介绍
  6. 直关的sql 联级更新语句
  7. hdu 5072 Coprime (容斥)
  8. HDU 1956 POJ 1637 Sightseeing tour
  9. 让 Python 带你进入开源的世界——Git 从入门到与他人协作开发
  10. 爬虫新手学习2-爬虫进阶(urllib和urllib2 的区别、url转码、爬虫GET提交实例、批量爬取贴吧数据、fidder软件安装、有道翻译POST实例、豆瓣ajax数据获取)
  11. Caused by:org.hibernate.MappingNotFoundException:resouce:com/you/model/Monkey.hbm.xml not found
  12. [Luogu2617]Dynamic Ranking
  13. Contest2158 - 2019-3-14 高一noip基础知识点 测试3 题解版
  14. PAT A1075 PAT Judge (25 分)——结构体初始化,排序
  15. linux 下创建虚拟环境 python
  16. UITextInputMode
  17. 【BFS】【余数剪枝】Multiple
  18. 2 自己编写:AppDelegate,CCApplication,CCApplicationProtocol
  19. Netty4具体解释三:Netty架构设计
  20. HackTwelve 为背景添加圆角边框

热门文章

  1. ORACLE常用性能监控SQL(二)
  2. LeetCode:全排列II【47】
  3. Python 中全局变量的实现
  4. PAT 天梯赛 L1-004. 计算摄氏温度 【水】
  5. iOS 调整图片尺寸,告诉你的UI,别问我尺寸!我要最大的
  6. 单元测试JUnit 4
  7. flex 客户端缓存SharedObject
  8. maven编译时GBK错误
  9. LeetCode——Sum of Two Integers
  10. aliyun阿里云Maven仓库地址——加速你的maven构建 - 转载