【问题标题】:JGit FetchCommandJGit 获取命令
【发布时间】:2012-09-07 15:16:55
【问题描述】:

我了解 Git fetch 的概念。我想通过 JGit API 中的 FetchCommand 类来获取对象。

我有一个 Git 存储库。例如C:\jGit\.git

我已经克隆了这个存储库。例如:C:\jGitClone\jGit\.git

我再次克隆了这个存储库。例如C:\Users\user\Desktop\jGitClone\.git

使用 JGit API 我有一个 Git 类:

Git git = new Git(repository); // this is the cloned repository. C:\jGitClone\jGit\.git

我将如何设置桌面上的克隆存储库,以便在调用 fetch 时知道要提取到此存储库中。

我查看了一些网站,包括下面的 1,但仍然卡住。

Git fetch failing using jgit: Remote does not have <branchname> available for fetch

【问题讨论】:

    标签: git jgit git-fetch


    【解决方案1】:

    我想你错过了什么。在 Git/jGit 中,本地副本由 path(就像在文件系统中一样)和 remote(远程仓库)确定。

    在您的情况下,您希望您的桌面克隆存储库具有指向您的jGitClone/jGit 存储库的链接。所以首先,如果克隆已经完成,那么桌面存储库就会知道它的远程存储库。因此,在您的代码中,您只需要说明您的桌面存储库在哪里:

    Builder builder = new FileRepositoryBuilder()
    Git repo = builder.setGitDir(new File("C:\Users\user\Desktop\jGitClone.git"))
      .readEnvironment()
      .build()
    

    代码可能无法直接工作(我没有测试它,我的疑问是在给出的路径上,以及要使用的对象:BuilderGit),但它基于这个答案我认为对你有帮助:JGit and finding the Head

    顺便说一句,你的问题不是很清楚:你是已经克隆了所有东西还是你想要做的?在您的代码中,您提到了 repository :您是否使用类似于我建议的代码获得它?您选择的名称也不是很清楚:调用 repo jGit,除非它包含 jGit 源代码,否则听起来有点笨拙。另外,为您的不同存储库(A、B、C)命名,以便更清楚地了解。

    【讨论】:

      【解决方案2】:

      您需要定义远程,它跟踪您可以从中获取提交的存储库,使用 valina git,您可以这样做:

      # suppose you have cloned C:\jGitClone\jGit\.git
      $ cd C:\Users\user\Desktop\jGitClone\.git
      $ git remote add upstream "C:\jGitClone\jGit\.git"
      $ git fetch upstream
      

      如果您想尝试git24j,您可以在 java 中执行此操作,就像使用 valina git 一样:

      Repository remote = Repository.open("C:\jGitClone\jGit\.git");
      Repository local = Repository.open("C:\Users\user\Desktop\jGitClone\.git");
      // set up remote
      Remote upstream = Remote.create(local, "upstream", URI.create(remote.getPath()));
      // fetch from remote
      upstream.fetch(null, null, "reflog");
      

      这两个nulls 是定义要获取的参考规范和选项(您可以在其中设置凭据等)的参数。

      【讨论】:

        猜你喜欢
        • 2012-11-04
        • 1970-01-01
        • 2015-01-29
        • 1970-01-01
        • 2015-07-27
        • 2015-07-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多