Reputation: 2472
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!
Upvotes: 0
Views: 1954
Reputation: 41
Be inspired by the people above, I've solved it. The following is my code:
/**
* 添加且提交全部
* 同等于 git add . && git commit -m 'some msg'
* @param repoDir 仓库地址
* @param msg 消息
*/
public static void addAndCommitAll(File repoDir, String msg) throws IOException, GitAPIException {
try(Git git=Git.open(repoDir)){
//全部添加, 除了.gitignore文件中指定的
doAddAll(git);
//全部提交
git.commit()
.setMessage(msg)
.call();
}
}
/**
* 添加全部
* 相当于 git add .
* @param repoDir 仓库地址
*/
public static void addAll(File repoDir) throws IOException, GitAPIException {
try(Git git=Git.open(repoDir)){
doAddAll(git);
}
}
private static void doAddAll(Git git) throws GitAPIException {
//add removed and modified file
git.add()
.setUpdate(true)
.addFilepattern(".")
.call();
//add new and modified file
git.add()
.addFilepattern(".")
.call();
}
Upvotes: 4
Reputation: 51
git.rm().addFilepattern("fileToDelete").call();
will only delete the files that are explicitly mentioned as the argument to addFilepattern().
Ideally, it should automatically delete all files that have been deleted from the local repo. And it can be done by using add().setUpdate(true) as shown below:-
git.add().setUpdate(true).addFilepattern(".").call();
git.commit().setMessage("delete files").call();
RefSpec spec = new RefSpec("refs/heads/master:refs/remotes/branch1");
git.push().setRemote("origin").setRefSpecs(spec).call();
Upvotes: 4
Reputation: 2472
This solve it for me:
git.pull();
git.rm().addFilepattern("fileToDelete").call();
commitChanges()
pushLocalChanges()
Upvotes: 2