我通过以下代码推送更改:
String folderPath = "files/filestoPush/"; String AddPattern = "";
List<String> listFiles =new ArrayList<>(); listFiles. add("file1");
AddPattern = folderPath + listFiles.get(0) + ".java" ;
Repository localRepo = new FileRepository(file.getAbsolutePath() + "/.git");
final Git git = new Git(localRepo);
git.add().addFilepattern(AddPattern).call();
String commitid =(git.commit().setMessage("Committed on : " + new Date()).call().getName()).toString();
PullCommand pullCmd = git.pull();
pullCmd.setTransportConfigCallback(new TransportConfigCallback() {
@Override
public void configure(Transport transport) {
SshTransport sshTransport = ( SshTransport )transport;
sshTransport.setSshSessionFactory( sshSessionFactory );
}
});
pullCmd.call();
Iterable<PushResult> prs = git.push().setTransportConfigCallback(new TransportConfigCallback() {
@Override
public void configure(Transport transport) {
SshTransport sshTransport = ( SshTransport )transport;
sshTransport.setSshSessionFactory( sshSessionFactory );
}
}).call();
此代码有时会添加所有更改以提交,而不是仅添加提供的addfilepattern。此addfile模式中提供了单个文件名的相对路径。