这里简单封装了一个工具类
package com.example.demo;
import org.eclipse.jgit.api.CloneCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import java.io.File;
public class GitUtils {
/**
* 创建git
* @param uri git地址
* @param branch 操作的分支
* @param directory 本地仓库目录
* @return
*/
public static Git createGit(String uri, String branch, String directory) {
try {
return new CloneCommand().setURI(uri)
.setBranch(branch)
.setDirectory(new File(directory))
.call();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 推送本地代码到远程
* @param git
* @param message 提交信息
* @param username 远程仓库用户名
* @param password 远程仓库密码
*/
public static void push(Git git, String message, String username, String password) {
try {
git.add().addFilepattern(".").call();
git.commit().setMessage(message).call();
git.pull().setCredentialsProvider(
new UsernamePasswordCredentialsProvider(username, password))
.call();
git.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
�