我正在使用java代码动态创建文件夹结构文件。创建文件夹和文件后,我想使用JGIT添加文件夹结构和下面的文件。我正在尝试使用以下代码
git.add().addFilepattern(folder.getName()).call();
git.add().addFilepattern(file.getName()).call();
但它只向git-reo添加文件。有没有其他方法可以使用JGIT 将文件夹添加到git中
这不是JGit造成的,而是Git本身的限制。默认情况下,它不存储文件夹,只存储文件。
所以你不能在Git中添加一个空文件夹。
作为解决方法,您可以添加一个伪文件(常用名称为.gitkeep
(或添加一个.gitignore
文件。
.gitignore:
Another way to make a directory stay (almost) empty (in the repository) is to create a .gitignore file inside that directory that contains these four lines:
# Ignore everything in this directory
*
# Except this file
!.gitignore
.gitkeep:
touch <dir>/.gitkeep
有关这些解决方案的详细描述,请参阅以下链接:
- 如何将空白目录添加到Git存储库
- https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/gitkeep-push-empty-folders-git-commit