【问题标题】:Push changes till particular commit to remote repository with JGit使用 JGit 推送更改直到特定提交到远程存储库
【发布时间】:2017-12-29 07:37:04
【问题描述】:

我可以使用 JGit 将所有本地提交推送到远程(裸存储库)。

RefSpec spec = new RefSpec("master:master");
Iterable<PushResult> resultIterable = git.push().setRemote("origin").setRefSpecs(spec).call();

现在我想将更改推送到特定的提交。下面是 git shell 命令,我需要等效的 JGit 实现。

git push <remotename> <commit SHA>:<remotebranchname>

【问题讨论】:

  • 它是否适用于spec = new RefSpec("&lt;commit SHA&gt;:&lt;remotebranchname&gt;");.setRemote("&lt;remotename&gt;")
  • 已尝试但出现以下错误:org.eclipse.jgit.api.errors.TransportException: remote: error: invalid protocol: Wanted 'old new ref'
  • 由于上述异常与 git shallow 有关,因此也尝试了此命令: $ git fetch --unshallow fatal: --unshallow on a complete repository doesn't sense

标签: java git jgit


【解决方案1】:

这是一个示例。我在 git-bash 中做了一些准备。

#init foo as the local repository
cd /e/
git init foo
#init bar as the remote repository
git init bar
#create two commits in the local repository
cd foo
> a
git add .
git commit -m 'root'
#the first sha1 is b8d35be23d9f4574c9da81cf2fddb4212daf92a1
> b
git add .
git commit -m 'ab'
#create a remote pointing to /e/bar
git remote add bar /e/bar

现在试试代码,模拟git push bar b8d35be23d9f4574c9da81cf2fddb4212daf92a1:refs/heads/newbranch

try {
        Git git = Git.open(new File("E:/foo/.git"));
        RefSpec spec = new RefSpec("b8d35be23d9f4574c9da81cf2fddb4212daf92a1:refs/heads/newbranch");
        git.push().setRemote("bar").setRefSpecs(spec).call();            
} catch (Exception e) {
        System.out.println(e);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-25
    • 2010-09-20
    • 2012-08-31
    • 2016-11-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多