Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Committing multiple files with github-api (PR #361 -
import org.kohsuke.github.*;
public class MultiCommit {
public static void main(String[] args) throws Exception {
String userId = "your-user-id";
String password = "your-password";
String repoName = "your-repo-name";
GitHub gitHub = GitHub.connectUsingPassword(userId, password);
GHRepository repo = gitHub.getRepository(repoName);
// get the reference to the master branch
GHRef masterRef = repo.getRef("heads/master");
// get the SHA of the latest commit on the master branch
String masterTreeSha = repo
.getTreeRecursive("master", 1)
// get an image as byte[]
byte[] avatarContent = IOUtils.toByteArray(
new URL("").openStream());
// create a blob containing the image and get its SHA
String avatarSha = new GHBlobBuilder(repo)
// create a tree with four entries (three text files and the previously created blob) and get its SHA
String treeSha = new GHTreeBuilder(repo)
.textEntry("README", "This is public domain software.", false)
.textEntry("doc/userGuide.txt", "Press F1 for help.", false)
.textEntry("bin/", "#/bin/sh\necho 'Hello!'", true) // executable
.shaEntry("avatar.png", avatarSha, false)
// create a commit for the new tree and get its SHA
String commitSha = new GHCommitBuilder(repo)
.message("add some important files")
// Update the master reference to refer to the new commit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.