JGit does not delete my file after I deleted from the local repository

advertisements

How can I delete a file using JGit? I deleted from the local repository and I commited-pushed the changes but seems like JGit did not noticed that it has been deleted. It still exists in the remote repository.

the function I call to commit changes:

public void commitChanges(){
        Git git = null;
        try {
            git = Git.open(new File("./TestGitRepository/.git") );
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        try {
            git.add().addFilepattern(".").call();
        } catch (NoFilepatternException e) {
            e.printStackTrace();
        } catch (GitAPIException e) {
            e.printStackTrace();
        }

        // Now, we do the commit with a message
        try {
            RevCommit revCommit= git.commit().setMessage("commit try").call();
        } catch (GitAPIException e) {
            e.printStackTrace();
        }
        git.getRepository().close();
    }

the function I call to push the changes:

public void pushLocalChanges(){
        Repository localRepo = null;
        try {
            localRepo = new FileRepository("./TestGitRepository/.git");
        } catch (IOException e) {
            e.printStackTrace();
        }
        Git git = new Git(localRepo);
        PushCommand push = git.push();
        UsernamePasswordCredentialsProvider user = new UsernamePasswordCredentialsProvider("userName", "password");
        push.setCredentialsProvider(user);
        push.setRemote(REMOTE_URL);
        try {
             push.call();
            System.out.println ("pushed to upstream: "+push.getReceivePack());
        } catch (GitAPIException e) {
            e.printStackTrace();
        }
        git.getRepository().close();
    }

Nothing is changing on the remote repository, What have I missed? Thanks in advance for your help!


This solve it for me:

 git.pull();
 git.rm().addFilepattern("fileToDelete").call();
 commitChanges()
 pushLocalChanges()