-1

I'm able to push all local commits to a remote (bare repository) using JGit.

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

Now I want to push changes till a particular commit. Below is the git shell command and I need the equivalent JGit implementation.

git push <remotename> <commit SHA>:<remotebranchname>
JJJ
  • 32,902
  • 20
  • 89
  • 102
prasadK
  • 3
  • 2
  • 2
    Does it work with `spec = new RefSpec(":");` and `.setRemote("")`? – ElpieKay Dec 29 '17 at 08:40
  • Tried but getting following error: org.eclipse.jgit.api.errors.TransportException: remote: error: invalid protocol: wanted 'old new ref' – prasadK Jan 02 '18 at 07:42
  • As above exception is related to git shallow, hence tried this command as well: $ git fetch --unshallow fatal: --unshallow on a complete repository does not make sense – prasadK Jan 02 '18 at 07:51

1 Answers1

2

Here is a sample. I did some preparation in 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

Now try the code, simulating 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);
}
ElpieKay
  • 27,194
  • 6
  • 32
  • 53