如何确定Git 仓库中最新提交的分支? 我只想克隆最近更新的分支而不是克隆所有分支,不管它是否 merge 到 master(默认分支)。
LsRemoteCommand remoteCommand = Git.lsRemoteRepository();
Collection <Ref> refs = remoteCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider(userName, password))
.setHeads(true)
.setRemote(uri)
.call();
for (Ref ref : refs) {
System.out.println("Ref: " + ref.getName());
}
//cloning the repo
CloneCommand cloneCommand = Git.cloneRepository();
result = cloneCommand.setURI(uri.trim())
.setDirectory(localPath).setBranchesToClone(branchList)
.setBranch("refs/heads/branchName")
.setCredentialsProvider(new UsernamePasswordCredentialsProvider(userName,password)).call();
谁能帮我解决这个问题?
最佳答案
恐怕您必须克隆整个存储库及其所有分支才能找到最新分支。
LsRemoteCommand
列出分支名称和它们指向的提交的 ID,但不包括提交的时间戳。
Git 的“一切都是本地的”设计要求您在检查其内容之前克隆一个存储库。注意:使用 Git/JGit 的低级命令/API 可以获取分支的头部提交进行检查,但这与其设计相矛盾。
一旦您克隆了存储库(无需初始 checkout ),您就可以遍历所有分支,加载相应的头提交并查看哪个是最新的。
下面的示例克隆了一个存储库及其所有分支,然后列出所有分支以找出它们各自的头部提交的时间:
Git git = Git.cloneRepository().setURI( ... ).setNoCheckout( true ).setCloneAllBranches( true ).call();
List<Ref> branches = git.branchList().setListMode( ListMode.REMOTE ).call();
try( RevWalk walk = new RevWalk( git.getRepository() ) ) {
for( Ref branch : branches ) {
RevCommit commit = walk.parseCommit( branch.getObjectId() );
System.out.println( "Time committed: " + commit.getCommitterIdent().getWhen() );
System.out.println( "Time authored: " + commit.getAuthorIdent().getWhen() );
}
}
现在您知道了最新的分支,您可以检查这个分支。
关于git - 在 JGit 中获取最新提交的分支(名称)详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40526743/