Search code examples
javagitjgit

Create orphan branch and rm -rf in JGit


I'm using JGit to create an orphan branch as outlined on this answer.

What is the equivalent command rm .git/index; git clean -fdx in JGit?

I'm able to create a new branch normally without the --orphan argument, but am unsure how to apply it as well as the command to delete the branched files, which you would do normally as git rm -rf .

I know that I can checkout an orphan branch, will it achieve the same thing? git.checkout().setOrphan(). Is it as simple as git.getRepository().getWorkTree().delete()?


Solution

  • As far as I know there is no direct equivalent to git clean -fdx in JGit. Your best bet is to manually prepare the work directory and index after you have created the orphan branch with JGit. Use the Java file system APIs to recursively delete all files from the work directory. Make sure not to delete the .git folder. And finally delete .git/index.