GitSubprocessClient gitSubprocessClient = new GitSubprocessClient(repoPath);
All GitSubprocessClient responses are Strings, and will be the exact output you would normally see from the terminal.
For that reason, they are a bit tricky to parse, but nothing that can’t be done with a little extra String manipulation code.
String methods split
and substring
are your friends!
These are literally identical to the Git terminal commands, just being run in a Java program.
String gitInit = gitSubprocessClient.gitInit();
Remote name and url must be supplied. Below will add a remote named “origin” with the url “https://github.com/CSC109/GitSubprocessClient.git”
String gitRemoteAdd = gitSubprocessClient.gitRemoteAdd("origin", "https://github.com/CSC109/GitSubprocessClient.git");
Stage a changed file – git add <filePath> command. A file path must be supplied.
String gitAddFile = gitSubprocessClient.gitAddFile("GitSubprocessClient.java");
Stage all changed file – git add . command.
String gitAddAll = gitSubprocessClient.gitAddAll();
Create a commit. A commit message must be supplied.
String commitMessage = "This is a commit message";
String commit = gitSubprocessClient.gitCommit(commitMessage);
Push all recent commits to the remote branch. A branch name must be specified.
String push = gitSubprocessClient.gitPush("master");
Gets the status of the repo.
String status = gitSubprocessClient.gitStatus();
Equivalent to running a git log to get commit information. The number of commits to log (starting from most recent) must be supplied.
Below will get the most recent 5 commits.
String log = gitSubprocessClient.gitLog(5);
Same as Git Log, but will condense the commit output text. Equivalent to running git log –oneline. The number of commits to log (starting from most recent) must be supplied.
Below will get the most recent 5 commits.
String log = gitSubprocessClient.gitLogOneLine(5);
Get all commit information.
String log = gitSubprocessClient.gitLogAll();
Same as Git Log All, but will condense the commit output text. Equivalent to running git log –oneline.
String log = gitSubprocessClient.gitLogAllOneLine();
Checkout to a new branch. Branch name to checkout to must be supplied.
String checkout = gitSubprocessClient.switchBranch("master");
Creates a branch. Does NOT checkout to branch. New branch name must be supplied.
String createBranch = gitSubprocessClient.createBranch("new-branch");
Creates a branch and performs a checkout to branch.
This is the git checkout -b /
String createBranch = gitSubprocessClient.createAndSwitchBranch("new-branch");
Deletes a branch. Branch name to delete must be supplied.
String deleteBranch = gitSubprocessClient.deleteBranch("new-branch");
Lists all branch names in repo.
String branches = gitSubprocessClient.listBranches();
Gets the name of the current branch the repo has checked out.
String branchName = gitSubprocessClient.getCurrentBranchName();
Performs a pull. Branch name to pull must be supplied.
String pull = gitSubprocessClient.gitPull("master");
Performs a fetch on a specific branch. Branch name to fetch must be supplied.
String fetch = gitSubprocessClient.gitFetch("master");
Performs a general fetch on all branches.
String fetch = gitSubprocessClient.gitFetchAll();
Stashes away all current changes that have not been committed yet. They will be “removed” temporarily, but can be reapplied later with Git Stash Apply.
String stash = gitSubprocessClient.gitStash();
Applies current stashed changes.
String stashApply = gitSubprocessClient.gitStashApply();
Clones remote repo to local machine. Url to clone must be supplied.
String clone = gitSubprocessClient.gitClone();
There is a method called runGitCommand
that can be used as a “catch all” to run any other Git command not already provided by the client.
For example, to run a git push origin master
using the runGitCommit
method, it would look like this:
String commit = gitSubprocessClient.runGitCommand("push origin master");