0

I am creating a folder structure files on the fly using java code. After creation of folder and files I want to add both folder structure and files underneath using JGIT. I am trying to use below code

  git.add().addFilepattern(folder.getName()).call(); 
  git.add().addFilepattern(file.getName()).call(); 

but it adds only files to git repo . Is there any different way to add folders to git using JGIT

centic
  • 15,565
  • 9
  • 68
  • 125
Peter
  • 405
  • 3
  • 6
  • 14
  • [Did you take a look at JGit's User Guide? There's a reference about that exact code snippet on it.](https://wiki.eclipse.org/JGit/User_Guide) – Jorge Luiz Mar 08 '22 at 23:11
  • Yes, I have followed section:AddCommand (git-add) in doc but still empty folder structure is not getting added – Peter Mar 09 '22 at 06:17

1 Answers1

1

This is not caused by JGit, but a limitation in Git itself. It does by default not store folders, only files.

So you cannot add an empty folder to Git.

As workaround you can add a dummy-file (a commonly used name is .gitkeep) or add a .gitignore file.

.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

See the following links with detailed description of these workarounds:

centic
  • 15,565
  • 9
  • 68
  • 125