Slippy Slippy - 1 year ago 139
Git Question

jgit - Delete .git directory (or get files without it)

I need to clone a git repo in java. I am using jGit.

The line of code I am using is :

Git clone = Git.cloneRepository().setURI(URIofRepo).setDirectory(localPath).call();

Where URIofRepo is : the github link to my repo
Where localPath is : the directory I want the clone to happen.

This works perfectly. However, since the use of the project I am doing doesn't require a clone for continued work, I simply want the clone to have the contents of the github repo WITHOUT the .git directory.

I tried also using the following :

File dirToDelete = new File (path + "/.git");

However I got an IO exception saying I am unable to delete the file as follows :

Exception in thread "main" Unable to delete file:

Answer Source

It is possible that the current eclipse process keep an handle on the pack file, preventing its deletion.
As RĂ¼diger Herrmann suggests in the comments:

If the open file handle is what prevents the file from being deleted, make sure to close the repository that is returned by init:


Another approach would be to download an archive of the GitHub repo through its archive link.

Or using JGit to create an archive from your current local repo (see, and unzip that archive for you to use.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download