使用 jGit 添加目录中的所有文件
Add all files in a directory using jGit
我想使用 addFilepattern
将指定目录中的所有文件添加到 git 存储库,然后提交更改。我希望在执行 git show
时看到提交中的文件,但提交存在但为空。
这是代码:
//create target directory for jGit
File targetDirectory = new File("/Users/asusti/jGit/");
if (!targetDirectory.exists()) {
if (targetDirectory.mkdir()) {
System.out.println("jGit directory is created!");
} else {
System.out.println("jGit Directory exists!");
}
}
//create a new Git repository instance
// The Git-object has a static method to initialize a new repository
try (Git git = Git.init()
.setDirectory(targetDirectory)
.call()) {
System.out.println("Created a new repository at " + git.getRepository().getDirectory());
git.add().addFilepattern("/Users/asusti/Downloads").call();
git.commit().setMessage("Added yaml files to the repository.").setAuthor("anas", "anas@email.com")
.call();
} catch (IllegalStateException | GitAPIException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
试一试
git.add().addFilepattern(".").call();
CommitCommand commit = git.commit();
commit.setMessage("Added yaml files to the repository.").call();
git.push().call();
我想使用 addFilepattern
将指定目录中的所有文件添加到 git 存储库,然后提交更改。我希望在执行 git show
时看到提交中的文件,但提交存在但为空。
这是代码:
//create target directory for jGit
File targetDirectory = new File("/Users/asusti/jGit/");
if (!targetDirectory.exists()) {
if (targetDirectory.mkdir()) {
System.out.println("jGit directory is created!");
} else {
System.out.println("jGit Directory exists!");
}
}
//create a new Git repository instance
// The Git-object has a static method to initialize a new repository
try (Git git = Git.init()
.setDirectory(targetDirectory)
.call()) {
System.out.println("Created a new repository at " + git.getRepository().getDirectory());
git.add().addFilepattern("/Users/asusti/Downloads").call();
git.commit().setMessage("Added yaml files to the repository.").setAuthor("anas", "anas@email.com")
.call();
} catch (IllegalStateException | GitAPIException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
试一试
git.add().addFilepattern(".").call();
CommitCommand commit = git.commit();
commit.setMessage("Added yaml files to the repository.").call();
git.push().call();