I'm using JGit to checkout a branch from my git repository and modify a file. After I committed the changes I try to push it but running into 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)
...
My code looks like this:
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);
}
Here is the method for checking out the current branch:
public void checkoutBranch(String branchName)
{
try
{
git.checkout().setName("origin/" + branchName).call();
}
catch(GitAPIException e)
{
throw new GitClientException("Cannot checkout branch.", e);
}
}
I looked for JGit examples and I found many of them, but no example handles file changes. Has anybody a hint what could be wrong?
答案 0 :(得分:1)
调用checkoutBranch
将导致HEAD脱离,这可能是push命令失败的原因。
git.getRepository().getFullBranch()
现在返回远程分支指向的提交的对象ID(SHA-1)。
要在不分离HEAD的情况下签出远程分支,Git需要创建一个充当代理并跟踪该远程分支的本地分支。有关如何使用JGit实现此功能的信息,请参见JGit: Checkout a remote branch。