如何使用 JGit 将删除的文件添加到索引中?
How can I use JGit to add deleted files to the index?
我想将文件夹中所做的所有修改添加到索引中:
- 已修改文件
- 已添加文件
- 已删除文件
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();
}
}
此命令的输出:
Missing: testGit/test.txt
和git status
的输出:
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: testGit/test.txt
no changes added to commit (use "git add" and/or "git commit -a")
删除的文件没有添加到索引中。我怎样才能做到这一点?
我查了下testAddRemovedCommittedFile,好像加删改不了
要添加一个已删除的文件到索引中,以便在提交时删除它,您需要使用RmCommand
.
就像您对本机所做的那样 Git
git rm test.txt
你可以在 JGit
git.rm().addFilepattern("test.txt").call();
这会将 test.txt
添加到索引并将其标记为要删除。提交的下一个修订将不再包含 test.txt
。
如果 addFilepattern()
指定的文件尚未从工作目录中删除,RmCommand
将删除它们。这是默认行为。在执行命令之前调用 setCached(true)
,将使工作目录不受影响。
我想将文件夹中所做的所有修改添加到索引中:
- 已修改文件
- 已添加文件
- 已删除文件
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();
}
}
此命令的输出:
Missing: testGit/test.txt
和git status
的输出:
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: testGit/test.txt
no changes added to commit (use "git add" and/or "git commit -a")
删除的文件没有添加到索引中。我怎样才能做到这一点?
我查了下testAddRemovedCommittedFile,好像加删改不了
要添加一个已删除的文件到索引中,以便在提交时删除它,您需要使用RmCommand
.
就像您对本机所做的那样 Git
git rm test.txt
你可以在 JGit
git.rm().addFilepattern("test.txt").call();
这会将 test.txt
添加到索引并将其标记为要删除。提交的下一个修订将不再包含 test.txt
。
如果 addFilepattern()
指定的文件尚未从工作目录中删除,RmCommand
将删除它们。这是默认行为。在执行命令之前调用 setCached(true)
,将使工作目录不受影响。