1引用jar
org.eclipse.jgit org.eclipse.jgit5.12.0.202106070339-r
2.代码示例:
package com.imobpay.ifeeling.web.controller;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.jgit.api.AddCommand;
import org.eclipse.jgit.api.CommitCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.internal.storage.file.FileRepository;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
@Slf4j
public class AgencyDeviceController {
public static void main(String[] args) throws GitAPIException, IOException {
//拉取代码
String path = "D:/test2/";//本地地址
String pathfile=path+".git";
Git git = new Git(new FileRepository(pathfile));//链接本地地址
git.pull().setRemoteBranchName("master").setCredentialsProvider(new UsernamePasswordCredentialsProvider("PA029002", "yuanshaohua000000"))
.call();//拉取代码
System.out.println("拉取代码成功");
String filePath = "D:/test2/" + "test2.txt";
FileWriter fw = null;
try {
File file = new File(filePath);
if (!file.exists()) {
file.createNewFile();
}
fw = new FileWriter(filePath);
BufferedWriter bw = new BufferedWriter(fw);
bw.write("hello3n");
bw.write("worldn");
bw.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
AddCommand add = git.add();
add.addFilepattern(".").call();
System.out.println("添加到git");
CommitCommand commit = git.commit();
commit.setMessage("这是测试2").call();
System.out.println("commit提交代码成功");
git.push()
.setCredentialsProvider(new UsernamePasswordCredentialsProvider("PA029002", "000000")) //身份验证
.call();
}
}



