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

如何为JGit中revert()创建的提交指定作者信息?

为JGit的RevertCommand指定提交作者信息

你已经通过JGit完成了一次带指定作者的提交,现在执行撤销操作时,希望让撤销生成的提交也使用和原提交相同的作者信息。

其实很简单,JGit的RevertCommand和你用的CommitCommand一样,提供了setAuthor()方法,直接调用该方法传入原提交的作者姓名和邮箱即可。

修改后的完整代码如下:

try (Git gitParent = Git.init().setDirectory(parentRepositoryRoot).call()) {
    final String fooName = "foo.txt";
    final String authorName = "foo";
    final String authorEmail = "<foo@example.com>";
    git.add().addFilepattern(fooName).call();
    RevCommit commit = git.commit()
        .setMessage(comment)
        .setAuthor(authorName, authorEmail)
        .setAll(true)
        .call();
    // 为RevertCommand指定作者信息
    git.revert()
        .include(commit)
        .setAuthor(authorName, authorEmail)
        .call();
}

这样设置后,撤销操作生成的新提交就会使用你指定的authorName和authorEmail,和原提交的作者信息保持一致。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 12:12:03