小编FGI*_*FGI的帖子

如何使用JGit将已删除的文件添加到索引中?

我想在索引中添加所有在文件夹中进行的修改:

  • 文件已修改
  • 文件已添加
  • 文件已删除
File gitDir = new File("/home/franck/Repositories/Git/Sandbox/.git");
try (Repository repo = new RepositoryBuilder().setGitDir(gitDir).build()){
    try (Git git = new Git(repo)){
        final Status status = git.status().addPath("testGit").call();
        final AddCommand addCommand = git.add();

        Set<String> removed = status.getRemoved();
        for (final String s : removed) {
            System.out.print("removed: "+s);
            addCommand.addFilepattern(s);
        }

        Set<String> missing =  status.getMissing();
        for (final String s : missing) {
            System.out.print("Missing: "+s);
            addCommand.addFilepattern(s);
        }

        addCommand.call();
    }
}
Run Code Online (Sandbox Code Playgroud)

输出此命令:

Missing: testGit/test.txt
Run Code Online (Sandbox Code Playgroud)

并输出git status:

On branch master  
Your branch is ahead of 'origin/master' …
Run Code Online (Sandbox Code Playgroud)

jgit

3
推荐指数
1
解决办法
1905
查看次数

标签 统计

jgit ×1