3

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.

hansi
  • 2,278
  • 6
  • 34
  • 42

1 Answers1

6

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.
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Ok, so should a localGit.checkout().setName("origin/development").setUpstreamMode(SetupUpstreamMode.TRACK).call(); do the trick? Because even then I get the same error message: TransportException: Nothing to push. – hansi Jul 04 '13 at 16:16
  • Also, when I do git status I see that the head still is detached. I don't know how I can fix that. – hansi Jul 04 '13 at 16:20
  • When I look into HEAD it is ref: refs/heads/development before I do the checkout. After the checkout I have 5750afe0339795f1e31c5c229842636f7db53357 (which is the reason for the detached head, right?) – hansi Jul 04 '13 at 16:56
  • took me a little bit longer to completely understand and make it work, but you gave me all the info needed. Thanks! – hansi Jul 04 '13 at 18:05
  • @hansi great! removing the checkout is the easiest solution, and avoid checkout of a remote tracking branch (which is the surest way to get a detached head and "nothing to push") – VonC Jul 04 '13 at 18:17