集成GIT倉庫
集成GIT倉庫
jgit - java實現git操作
一個 Java 程序中使用 Git ,有一個功能齊全的 Git 庫,那就是 JGit 。 JGit 是一個用 Java 寫成的功能相對健全的 Git 的實現,它在 Java 社區(qū)中被廣泛使用, JGit 項目由 Eclipse 維護。
官網地址
倉庫地址
https://gitee.com/mirrors/jgit
例子
https://github.com/centic9/jgit-cookbook
實例代碼
代碼示例:https://wiki.eclipse.org/JGit/User_Guide#git-add
依賴:
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit-parent</artifactId>
<version>6.1.0.202203080745-r</version>
<type>pom</type>
</dependency>
功能代碼:
//官方代碼
Repository repository = new FileRepositoryBuilder()
.setGitDir(new File("C:\\..\\.git"))
.readEnvironment()
.findGitDir() // scan up the file system tree
.build();
// 一般選擇封裝成方法
public static Repository getRepository(String dir) {
try {
Repository repository = new FileRepositoryBuilder()
.setGitDir(new File("C:\\..\\.git"))
.readEnvironment()
.findGitDir()
.build();
return repository;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
獲取GIT對象
拿到git對象后就可以去執(zhí)行git操作了
public static Git getGitObj(Repository repository) {
Git git = null;
git = new Git(repository);
return git;
}

這里面最常用的比如切換分支,新建分支,刪除分支,revet,reset,commit,add。
官網操作代碼:
Git git = new Git(db);
AddCommand add = git.add();
add.addFilepattern("需要add的文件名或者文件目錄").call();
Git git = new Git(db);
CommitCommand commit = git.commit();
commit.setMessage("initial commit").call();
Git git = new Git(db);
RevCommit commit = git.commit().setMessage("initial commit").call();
RevTag tag = git.tag().setName("tag").call();
Git git = new Git(db);
Iterable<RevCommit> log = git.log().call();
1.new branch from …
需要先checkout base的分支然后create。
git.checkout().setName(baseBranch).call();
git.branchCreate().setName(新分支名).call();
//切換到新分支
git.checkout().setName(新分支名).call();
2.add&commit
git.add().addFilepattern(".").call();
git.rm().addFilepattern(".").call()
//setall()對應 -a 命令
git.commit().setMessage(commit msg).setAll(false).call();
// 一般的在add之前還會需要查看文件status,然后決定add哪些或者rm哪些
Status status = git.status().call();
Map<String,String> map = new HashMap<String,String>();
map.put("Added", status.getAdded().toString());
map.put("Changed", status.getChanged().toString());
map.put("Conflicting", status.getConflicting().toString());
map.put("ConflictingStageState", status.getConflictingStageState().toString());
map.put("IgnoredNotInIndex", status.getIgnoredNotInIndex().toString());
map.put("Missing", status.getMissing().toString());
map.put("Modified", status.getModified().toString());
map.put("Removed", status.getRemoved().toString());
map.put("UntrackedFiles", status.getUntracked().toString());
map.put("UntrackedFolders", status.getUntrackedFolders().toString());
System.out.println(map);
3.revert
使用git對象revert時需要拿到你當初commit的commitid或者其他操作的objectid。
git.revert().include(ObjectId.fromString(commitId)).setOurCommitName("OURS").call();
輸入commitid然后操作。但是不可能誰能記住哪次commit的commitid,所以這里選擇通過log去獲取Revcommit對象。
List<String> commitIdList = new LinkedList<>();
gitObj.log().call().forEach(e -> finalPluginCommitLogList.add(e.getName()));
posted on 2022-12-22 15:23 Chase_Hanky 閱讀(120) 評論(0) 收藏 舉報
浙公網安備 33010602011771號