gpt4 book ai didi

java - 从本地存储库中删除文件后,JGit 没有删除我的文件

转载 作者:行者123 更新时间:2023-12-01 21:55:04 25 4
gpt4 key购买 nike

如何使用 JGit 删除文件?

我从本地存储库中删除了并提交了更改,但似乎 JGit 没有注意到它已被删除。它仍然存在于远程存储库中。

我调用提交更改的函数:

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();
}

我调用的函数来推送更改:

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();
}

远程存储库上没有任何变化,我错过了什么?预先感谢您的帮助!

最佳答案

git.rm().addFilepattern("fileToDelete").call(); 只会删除作为 addFilepattern() 参数明确提及的文件。

理想情况下,它应该自动删除已从本地存储库中删除的所有文件。可以通过使用 add().setUpdate(true) 来完成,如下所示:-

            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();

关于java - 从本地存储库中删除文件后,JGit 没有删除我的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34497478/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com