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

使用JGit修改仓库提交推送后,远程仓库无变更问题排查

问题解决:JGit提交后远程仓库无文件变更

问题现象

本地克隆仓库内文件创建、修改操作均正常,但执行commit和push后,远程仓库仅显示空提交(提示“显示0个已更改文件,0次添加和0次删除”),无实际内容同步。

原代码

postbody = usabled;
String pathToClone = "./repo";
Git git = Git.cloneRepository()
        .setURI("https://github.com/Glitch31415/rws.git")
        .setDirectory(new File(pathToClone))
        .call();
System.out.println("remade local repo");
if (windows == true) {
    new File(git.getRepository().getDirectory().getParent() + "\\community\\", postname);
}
else {
    new File(git.getRepository().getDirectory().getParent() + "/community/", postname);
}

try {
    FileWriter myWriter;
    if (windows == true) {
        myWriter = new FileWriter(git.getRepository().getDirectory().getParent() + "\\community\\" + postname);
        System.out.println("writing the file to " + git.getRepository().getDirectory().getParent() + "\\community\\" + postname);
    }
    else {
        myWriter = new FileWriter(git.getRepository().getDirectory().getParent() + "/community/" + postname);
        System.out.println("writing the file to " + git.getRepository().getDirectory().getParent() + "/community/" + postname);
    }

    myWriter.write(postbody);
    System.out.println("wrote " + postbody);
    myWriter.close();
} catch (IOException e) {
    e.printStackTrace();
}
git.add().addFilepattern(postname).call();
try {
    try {
        File myObj;
        if (windows == true) {
              myObj = new File(git.getRepository().getDirectory().getParent() + "\\community\\index");
        }
        else {
              myObj = new File(git.getRepository().getDirectory().getParent() + "/community/index");
        }

          Scanner myReader = new Scanner(myObj);
          while (myReader.hasNextLine()) {
            st = st + myReader.nextLine() + "\n";
          }
          myReader.close();
        } catch (FileNotFoundException e) {
          e.printStackTrace();
        }
    System.out.println("index was read as '" + st + "'");
    FileWriter myWriter;
    if (windows == true) {
        myWriter = new FileWriter(git.getRepository().getDirectory().getParent() + "\\community\\index");
    }
    else {
        myWriter = new FileWriter(git.getRepository().getDirectory().getParent() + "/community/index");
    }

    
    myWriter.write(st+postname+"\n");
    myWriter.close();
} catch (IOException e) {
    e.printStackTrace();
}
git.add().addFilepattern("/community/index").call();
git.commit().setMessage("Committed from server").call();
PushCommand pushCommand = git.push();
pushCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider("key", ""));
pushCommand.call();
Path directory = Path.of(pathToClone);
Files.walk(directory)
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);

错误原因与修复方案

1. Git添加文件的路径错误

JGit的addFilepattern方法接收的是相对于仓库根目录的相对路径,而非单纯文件名或带前置斜杠的路径:

  • 原代码中git.add().addFilepattern(postname).call()仅传入文件名,但文件实际在community/目录下,Git无法定位到该文件,因此未添加到暂存区。
  • 第二个addFilepattern("/community/index")的前置斜杠是无效格式,Git不识别此类路径,需改为相对路径community/index。

2. 路径拼接优化(可选但推荐)

硬编码\\或/易引发跨平台问题,建议用File类的层级构造或File.separator拼接路径,避免手动处理分隔符。

修正后的关键代码片段

// 获取仓库根目录
File repoRoot = git.getRepository().getDirectory().getParentFile();
// 确保community目录存在
new File(repoRoot, "community").mkdirs();

// 处理新文件
File newPostFile = new File(new File(repoRoot, "community"), postname);
try (FileWriter myWriter = new FileWriter(newPostFile)) {
    myWriter.write(postbody);
    System.out.println("wrote " + postbody);
} catch (IOException e) {
    e.printStackTrace();
}
// 正确添加文件:使用仓库根目录下的相对路径
git.add().addFilepattern("community/" + postname).call();

// 处理index文件
File indexFile = new File(new File(repoRoot, "community"), "index");
try {
    StringBuilder st = new StringBuilder();
    try (Scanner myReader = new Scanner(indexFile)) {
        while (myReader.hasNextLine()) {
            st.append(myReader.nextLine()).append("\n");
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    System.out.println("index was read as '" + st + "'");
    try (FileWriter myWriter = new FileWriter(indexFile)) {
        myWriter.write(st.append(postname).append("\n").toString());
    }
} catch (IOException e) {
    e.printStackTrace();
}
// 正确添加index文件
git.add().addFilepattern("community/index").call();

// 提交与推送
git.commit().setMessage("Committed from server").call();
PushCommand pushCommand = git.push();
pushCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider("key", ""));
pushCommand.call();

额外注意点

  • 操作文件前需确保community目录存在,可调用mkdirs()自动创建层级目录
  • 使用try-with-resources语法自动关闭流,避免资源泄漏

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 17:42:02