You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用JGit库执行提交操作后如何获取对应commit id

JGit获取本次提交Commit ID解决方案

你使用的JGit CommitCommand 的call()方法执行成功后会直接返回本次提交对应的RevCommit实例,无需事后查询最新提交,天然规避了多进程并发提交导致的ID匹配错误问题。

修改后的代码示例如下:

CommitCommand commitCommand = git.commit();
commitCommand.setMessage("My change");
commitCommand.setAuthor(name, name + "@xyz.com");
// 接收call方法返回的本次提交对应RevCommit对象
RevCommit currentCommit = commitCommand.call();
// 获取Commit ID的ObjectId对象
ObjectId commitId = currentCommit.getId();
// 获取字符串格式的完整40位Commit ID
String fullCommitId = commitId.getName();
// 如需常用的7位短Commit ID,直接截取即可
String shortCommitId = fullCommitId.substring(0,7);

该方案拿到的Commit ID完全对应你本次执行的提交操作,和其他进程的提交行为完全隔离,不会出现匹配错误的问题。

内容的提问来源于stack exchange,提问作者arielBodyLotion

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 02:51:04