簡體   English   中英

使用 JGit 推送更改直到特定提交到遠程存儲庫

[英]Push changes till particular commit to remote repository with JGit

我能夠使用 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>

這是一個示例。 我在 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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM