參考:https://blog.csdn.net/luckyrocks/article/details/107671468
https://stackoverflow.com/questions/45913082/jgit-addfilepattern-not-working-for-my-absolute-path-to-a-file
問題:我在windows 使用jGit操作git ,git add 檔案沒有添加到git管理,git目錄是D:\\aa ,我想添加D:\\aa\\bb檔案夾及其所有子檔案
AddCommand addCommand = git.add();
addCommand.addFilepattern("D:\\aa\\bb").call();\\無效
正確用法:使用相對路徑,如果有子目錄,使用 / 作為路徑分隔符
AddCommand addCommand = git.add();
addCommand.addFilepattern("bb").call();
原因:addCommand 暫不支持 * ,filePath/. 等遞回操作,且只支持相對路徑和 / 作為分隔符的路徑
JGit 提交完整代碼,執行前先確定當前環境git pull 能不能成功,有沒有沖突,如果想使用jGit做沖突處理,請參考其他代碼,
Git git;
try {
git = new Git(new FileRepository(LOCALGITFILE));//對應值為D://aa/.git
PullResult pullResult = git.pull().setCredentialsProvider(new UsernamePasswordCredentialsProvider(USER, PASSWORD)).call();
System.out.println(pullResult.toString());
AddCommand addCommand = git.add();
addCommand.addFilepattern("bb").call();//對應路徑為D://aa//bb
git.commit().setMessage("java commit").call();
Iterable<PushResult> pushResults = git.push().setCredentialsProvider(new UsernamePasswordCredentialsProvider(USER, PASSWORD)).call();
pushResults.forEach(pushResult -> {
System.out.println(pullResult.toString());
});
} catch (IOException | GitAPIException e) {
e.printStackTrace();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/184137.html
標籤:其他
