使用JGit推送更改,直到特定提交到远程存储库

问题描述 投票:-1回答:1

我可以使用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>
java git jgit
1个回答
0
投票

这是一个例子。我在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);
}
© www.soinside.com 2019 - 2024. All rights reserved.