JGit sparse checkout keeps adding files

In Java, I’m working with JGit for performing some actions with a remote repository. However when I sequentially do a sparse checkout between versions with:


It keeps the last checkout file and adds new one. It only happens when the name of the files inside the directory are not the same. How could I avoid that when using checkout command this way?

I thought about deleting files inside folder that is being checkout (scripts, in this case), however I don’t know if it will bring conflict when the file being ‘downloaded’ has the same name.

  • Use jgit to do git fetch -p
  • Jenkins git-client: how to move to jgit on windows
  • How to use JGit to get the URL of origin?
  • Determine if a branch is remote or local (using JGit)?
  • JGIT Pull NoHeadException
  • Get original commit info from Merged commit
  • gpg signing support for JGIT
  • Why is not recommended to have an Eclipse project folder as a Git repository?
  • One Solution collect form web for “JGit sparse checkout keeps adding files”

    First note that setCreateBranch() is false by default, hence there is no need to explicitly call setCreateBranch( false ).

    Furthermore, you cannot mix addPath() and setName() The JavaDoc for addPath() says:

    If this option is set, neither the setCreateBranch(boolean) nor setName(String) option is considered. In other words, these options are exclusive.

    However, I am not sure if the behavior that you see is correct. In doubt check with command-line git to see if it shows the same results and file a bug report if JGit differs.

    To work around the orphan files, you can use the StatusCommand to manually delete all untracked files:

    Status status = git.status().call();
    for( String fileName : status.getUntracked() ) {
      // delete fileName
    Git Baby is a git and github fan, let's start git clone.