【问题标题】:JGit: Push to specific branchJGit:推送到特定分支
【发布时间】:2013-07-03 21:45:31
【问题描述】:

我在 github 上有两个分支:master 和 development。 我想将新创建的文件推送到开发分支。

    String user = "user";
    String password = "password";
    String localPath = "local";
    String remotePath = "https://github.com/some/git.git";
    Git.cloneRepository().setBranch("refs/heads/development").setURI(remotePath).setDirectory(new File(localPath)).call();
    Git localGit = Git.open(new File(localPath));         
    localGit.checkout().setName("origin/development").setUpstreamMode(SetupUpstreamMode.TRACK).call();

    new File("local/test").createNewFile();

    localGit.add().addFilepattern(".").call();
    localGit.commit().setMessage("message").call();
    localGit.push().setCredentialsProvider(new UsernamePasswordCredentialsProvider(user, password)).call();

我得到的是一个

  TransportException: Nothing to push.

那里有什么问题?

更新: 我可以通过删除 checkout 命令使其工作。由于克隆已经签出了指定的分支,这在我之前并不清楚。

【问题讨论】:

    标签: git branch push jgit


    【解决方案1】:

    通过检查origin/development(即upstream repo 'origin'的remote tracking branch),您创建了一个detached head,即与0 相关联的头部local 分支(即,不使用refs/head/development,但这里使用refs/remotes/origin/development

    这就是为什么 git push 返回“无推送”的原因,因为没有本地分支收到任何新的推送提交。

    您需要确定:

    • 签出本地 'development' 分支(您使用 .setBranch("development") 执行此操作,尽管我更喜欢在 jgit clone test 中使用 .setBranch("refs/head/development") 以确保引用本地分支)李>
    • 使“origin/development”成为“development”的上游分支。
      或者至少,就像在这个 jgit push test 中一样,使用正确的 refspec development:development

    【讨论】:

    • 好的,localGit.checkout().setName("origin/development").setUpstreamMode(SetupUpstreamMode.TRACK).call();做这个把戏?因为即使那样我也会收到相同的错误消息:TransportException: Nothing to push。
    • 另外,当我执行 git status 时,我看到头部仍然是分离的。我不知道该如何解决。
    • 当我查看 HEAD 时,它是 ref: refs/heads/development,然后再进行结帐。结帐后我有 5750afe0339795f1e31c5c229842636f7db53357 (这是分离头的原因,对吧?)
    • 我花了一点时间才完全理解并让它发挥作用,但你给了我所有需要的信息。谢谢!
    • @hansi 太棒了!删除结帐是最简单的解决方案,并避免结帐远程跟踪分支(这是获得分离头和“无所事事”的最可靠方法)
    猜你喜欢
    • 2013-10-09
    • 2016-06-06
    • 2011-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-26
    相关资源
    最近更新 更多