gpt4 book ai didi

java - 使用 JGit 推送更改的文件时遇到 TransportException "Nothing to push"

转载 作者:行者123 更新时间:2023-12-02 10:59:00 25 4
gpt4 key购买 nike

我正在使用 JGit 从我的 git 存储库中 checkout 分支并修改文件。提交更改后,我尝试推送它,但遇到了 TransportException:

Caused by: org.eclipse.jgit.errors.TransportException: Nothing to push.
at org.eclipse.jgit.transport.Transport.push(Transport.java:1332)
at org.eclipse.jgit.api.PushCommand.call(PushCommand.java:169)
...

我的代码如下所示:

this.checkoutBranch(branchName);
try (Writer writer = new OutputStreamWriter(new FileOutputStream(getFilePath(filepath).toFile(), true),
StandardCharsets.UTF_8))
{
Yaml yaml = this.initializeYaml();
Map<String, Object> parameterMap = this.getParameterMap(filepath, yaml);
parameterMap.put(key, value);

// write file
yaml.dump(parameterMap, writer);

// commit and push
git.add().addFilepattern(filepath).call();
git.commit().setMessage("Added parameter with key: '" + key + "'").call();

git.push().setCredentialsProvider(getCredentialProvider()).call();
}
catch(GitAPIException | IOException e)
{
throw new GitClientException("Cannot write parameters.", e);
}

以下是检查当前分支的方法:

public void checkoutBranch(String branchName)
{
try
{
git.checkout().setName("origin/" + branchName).call();
}
catch(GitAPIException e)
{
throw new GitClientException("Cannot checkout branch.", e);
}
}

我查找了 JGit 示例,发现了很多,但没有示例处理文件更改。有人提示可能出了什么问题吗?

最佳答案

调用checkoutBranch将导致HEAD分离,这可能是push命令失败的原因。

git.getRepository().getFullBranch() 现在返回远程分支指向的提交的对象 ID (SHA-1)。

要在不分离 HEAD 的情况下 check out 远程分支,Git 需要创建一个本地分支作为代理并跟踪远程分支。请参阅JGit: Checkout a remote branch了解如何使用 JGit 实现此目的。

关于java - 使用 JGit 推送更改的文件时遇到 TransportException "Nothing to push",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51496146/

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