Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I do git push with JGit?

Tags:

I'm trying to build a Java application that allows users to use Git based repositories. I was able to do this from the command-line, using the following commands:

git init <create some files> git add . git commit git remote add <remote repository name> <remote repository URI> git push -u <remote repository name> master 

This allowed me to create, add and commit content to my local repository and push contents to the remote repository. I am now trying to do the same thing in my Java code, using JGit. I was able to easily do git init, add and commit using JGit API.

Repository localRepo = new FileRepository(localPath); this.git = new Git(localRepo);         localRepo.create();   git.add().addFilePattern(".").call(); git.commit().setMessage("test message").call(); 

Again, all of this works fine. I couldn't find any example or equivalent code for git remote add and git push. I did look at this SO question.

testPush() fails with the error message TransportException: origin not found. In the other examples I've seen https://gist.github.com/2487157 do git clone before git push and I don't understand why that's necessary.

Any pointers to how I can do this will be appreciated.

like image 482
John Smith Avatar asked Nov 19 '12 02:11

John Smith


People also ask

How do I create a git push?

Do one of the following: To push changes from the current branch press Ctrl+Shift+K or choose Git | Push from the main menu. To push changes from any local branch that has a remote, select this branch in the Branches popup and choose Push from the list of actions.

Can I git push without pulling?

You can do a forced push. The forced push will erase all commit history of the remote repository's branch, and replace it to your branch. Check the answers for doing forced pushes in "How do I properly force a Git push?".


2 Answers

The easiest way is to use the JGit Porcelain API:

    Git git = Git.open(localPath);       // add remote repo:     RemoteAddCommand remoteAddCommand = git.remoteAdd();     remoteAddCommand.setName("origin");     remoteAddCommand.setUri(new URIish(httpUrl));     // you can add more settings here if needed     remoteAddCommand.call();      // push to remote:     PushCommand pushCommand = git.push();     pushCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider("username", "password"));     // you can add more settings here if needed     pushCommand.call(); 
like image 98
OlgaMaciaszek Avatar answered Oct 10 '22 12:10

OlgaMaciaszek


You will find in org.eclipse.jgit.test all the example you need:

  • RemoteconfigTest.java uses Config:

    config.setString("remote", "origin", "pushurl", "short:project.git"); config.setString("url", "https://server/repos/", "name", "short:"); RemoteConfig rc = new RemoteConfig(config, "origin"); assertFalse(rc.getPushURIs().isEmpty()); assertEquals("short:project.git", rc.getPushURIs().get(0).toASCIIString()); 
  • PushCommandTest.java illustrates various push scenario, using RemoteConfig.
    See testTrackingUpdate() for a complete example pushing an tracking a remote branch.
    Extracts:

    String trackingBranch = "refs/remotes/" + remote + "/master"; RefUpdate trackingBranchRefUpdate = db.updateRef(trackingBranch); trackingBranchRefUpdate.setNewObjectId(commit1.getId()); trackingBranchRefUpdate.update();  URIish uri = new URIish(db2.getDirectory().toURI().toURL()); remoteConfig.addURI(uri); remoteConfig.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/"     + remote + "/*")); remoteConfig.update(config); config.save();   RevCommit commit2 = git.commit().setMessage("Commit to push").call();  RefSpec spec = new RefSpec(branch + ":" + branch); Iterable<PushResult> resultIterable = git.push().setRemote(remote)     .setRefSpecs(spec).call(); 
like image 36
VonC Avatar answered Oct 10 '22 13:10

VonC