JGit: Push to specific branch

branch, git, jgit, push

Solution

By doing a checkout of the `origin/development` (that is a remote tracking branch of the upstream repo '`origin`'), you have created a detached head, that is a head associated with 0 local branch (ie, not with `refs/head/development`, but here with `refs/remotes/origin/development`)

That is why a `git push` returns "nothing to push", since no local branch has received any new commit to push.

You need to be sure of:

- having the local '`development`' branch checked out (which you did with `.setBranch("development")`, although I prefer, as in the jgit clone test, to use `.setBranch("refs/head/development")` to be sure to reference a local branch)

- making '`origin/development`' the upstream branch of '`development`'. Or at least, as in this jgit push test, use the right refspec `development:development`.

Problem

I have two branches on github: master and development. I want to push a newly created file to the development branch. ``` String user = "user"; String password = "password"; String localPath = "local"; String remotePath = "https://github.com/some/git.git"; Git.cloneRepository().setBranch("refs/heads/development").setURI(remotePath).setDirectory(new File(localPath)).call(); Git localGit = Git.open(new File(localPath)); localGit.checkout().setName("origin/development").setUpstreamMode(SetupUpstreamMode.TRACK).call(); new File("local/test").createNewFile(); localGit.add().addFilepattern(".").call(); localGit.commit().setMessage("message").call(); localGit.push().setCredentialsProvider(new UsernamePasswordCredentialsProvider(user, password)).call(); ``` What I get is a ``` TransportException: Nothing to push. ``` What's the problem there? Update: I could make it work by removing the checkout command. As the clone already checks out the specified branch, which wasn't clear to me before.

Original source

Related problems