I have used below code to do push.
public static void main(String[] args) throws IOException,GitAPIException
{
Repository localRepo = new
FileRepository("C:\\Users\\Joshi\\Desktop\\demo");
Git git = new Git(localRepo);
// add remote repo:
RemoteAddCommand remoteAddCommand = git.remoteAdd();
remoteAddCommand.setName("origin");
try {
remoteAddCommand.setUri(new
URIish("https://bitbucket.org/nidhi011/bxc"));
System.out.println("file Added");
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// you can add more settings here if needed
remoteAddCommand.call();
git.commit().setMessage( "commited" ).call();
// push to remote:
PushCommand pushCommand = git.push();
pushCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider("username", "password"));
// you can add more settings here if needed
pushCommand.call();
}
and my error is
file Added
Exception in thread "main"
org.eclipse.jgit.api.errors.WrongRepositoryStateException: Cannot commit on
a repo with state: BARE
at org.eclipse.jgit.api.CommitCommand.call(CommitCommand.java:171)
at maven_git.push.main(push.java:38)
After running the above code I got this exception error please help me to solve out the jgit push command. And yes One more thing when I exceutes this code it make config file in my local directory " demo" folder I can't understand that.
There're several lines that are incorrect in your code. Let's take a look together.
Opening a Git repository using FileRepository is tricky. This is an internal API, where the given string is the location of the repository metadata (the .git folder). In other words, it is used to construct a Git bare repository. You can prove it by calling the Repository#isBare():
Repository localRepo = new FileRepository("C:\\Users\\Joshi\\Desktop\\demo");
System.out.println(localRepo.isBare()); // true
After using this API, the created repository is a BARE repository. You cannot commit to a bare repository because it doesn't have a workspace. That's why you've exception saying:
org.eclipse.jgit.api.errors.WrongRepositoryStateException: Cannot commit on a repo with state: BARE
A better way is to use Git#open(). Note that you should close the Git repository after using it. So I'm using the try-with-resources statement here:
try (Git git = Git.open(new File("C:\\Users\\Joshi\\Desktop\\demo"))) {
// Add your logic here ...
}
Before commit the changes, you need to add them to the index, to prepare the content staged for the next commit. For example:
git.add().addFilepattern(".").call();
Notice that this is completely different from RemoteAddCommand: we are adding file content, while RemoteAddCommand adds a new remote URL. In native Git commands, they're respectively:
git add .
git remote add origin https://bitbucket.org/nidhi011/bxc
You're correct about this part.
If the local branch is not checked out from a tracking branch, then you would need to precise the remote branch name in your push-command.
git.push().setRemote("origin").add("master").call();
If the credentials were incorrect, user would not be authorized for pushing the changes. In this case, a TransportException will be thrown. You can add additional logic for exception handling:
try {
git.push().setRemote("origin").add("master").call();
} catch (TransportException e) {
// Add your own logic here, for example:
System.out.println("Username or password incorrect.");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With