用JGit推送更改的文件时

用JGit推送更改的文件时

本文介绍了遇到TransportException“没什么可推动的"用JGit推送更改的文件时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JGit从git存储库中检出一个分支并修改文件.提交更改后,我尝试将其推送,但遇到TransportException:

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)
...

我的代码如下:

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

我查找了JGit示例,但发现了很多示例,但没有示例处理文件更改.有没有人暗示什么可能是错的?

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?

推荐答案

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

Calling checkoutBranch will result in a detached HEAD and this may be the reason why the push command fails.

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

git.getRepository().getFullBranch() now returns the object id (SHA-1) of the commit to which the remote branch points to.

要在不分离HEAD的情况下签出远程分支,Git需要创建一个充当代理并跟踪该远程分支的本地分支.请参阅 JGit:检出远程分支,了解如何使用JGit实现此目的.

To check out a remote branch without detaching HEAD, Git needs to create a local branch that acts as a proxy and tracks the remote branch. See JGit: Checkout a remote branch for how to achieve this with JGit.

这篇关于遇到TransportException“没什么可推动的"用JGit推送更改的文件时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 11:18