Use JGit to perform clone, add, commit and push Git operations with Bitbucket access tokens.
I’m using Eclipse for development with Bitbucket Access Tokens. I found that the 2022-03 version of Eclipse was able to clone Bitbucket repositories using Access Tokens. I found that the ‘Clone URI’ operation using Bitbucket Access Tokens did NOT work for the following versions of Eclipse: 2024-09, 2024-12, 2025-12, 2026-03. I was also unable to use the Bitbucket ‘git clone’ command provided when an access token was created in the Windows Command Prompt interface:
The response message was:
- Cloning into 'repositoryname'...
- remote: You may not have access to this repository or it no longer exists in this workspace. If you think this repository exists and you have access, make sure you are authenticated.
- fatal: Authentication failed for 'https://bitbucket.org/username/repositorylowercase.git/'
I performed the following searches on Chrome:
- Bitbucket why doesn't clone URI in Eclipse work with tokens
- How to make JGIT correctly parse Bitbucket tokens
- How to use jgit to perform git add with bitbucket token
The first search initially gave the following response (but it did not respond the same way with subsequent tries):
- Bitbucket clone URIs often fail in Eclipse because JGit (the Git engine Eclipse uses) cannot natively parse modern Bitbucket API tokens passed directly in the clone URL.
The two JGIT searches provided sample code which I packaged into a Java Swing interface application which performs all the basic operations. The code below shows the code that implements these operations. I hope that some may find this useful.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
Code Start
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
package com.bitbucket;
import java.io.File;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
public class BitbucketMessage {
public void parseUriCloneAddCommitPush() throws Exception {
File localRepositoryDirectory = null;
Git git = null;
String bitbucketAddress = null;
String commitMessage = null;
String folderNameLc = null;
String httpHeader = null;
String localRepositoryLocation = null;
String remoteUri = null;
String token = null;
String uri = null;
String workspaceFolderPath = null;
String workspaceName = null;
String xTokenAuth = null;
String[] arrayAtSign = null;
String[] arrayColon = null;
String[] arrayDoubleSlash = null;
try {
//-----------
// Parse URI
//-----------
//Set workspace (folder) name
workspaceName = "WorkspaceName";
//Set local repository location
workspaceFolderPath = "C:/.../.../.../" + workspaceName;
//Convert to lower case
folderNameLc = workspaceName.toLowerCase();
//Set local repository location
localRepositoryLocation = workspaceFolderPath + "/" + folderNameLc;
//Get uri
uri = "git clone https://x-token-auth:<TOKEN>@bitbucket.org/username/repositoryLowerCase.git";
//Get segment arrays
arrayDoubleSlash = uri.split("://", -1);
arrayColon = arrayDoubleSlash[1].split(":", -1);
arrayAtSign = arrayColon[1].split("@", -1);
//Get base values
httpHeader = arrayDoubleSlash[0].replace("git clone", "").trim();
xTokenAuth = arrayColon[0].trim();
token = arrayAtSign[0].trim();
bitbucketAddress = arrayAtSign[1].trim();
//Prepare values for clone operation
httpHeader = httpHeader + "://";
xTokenAuth = xTokenAuth + ":";
bitbucketAddress = "@" + bitbucketAddress;
//Construct remote uri
remoteUri = httpHeader + xTokenAuth + token + bitbucketAddress;
//-----------
// Clone
//-----------
//Clone repository
Git.cloneRepository()
.setURI(remoteUri)
.setDirectory(new File(localRepositoryLocation))
.call();
//-----------
// Add, Commit, Push
//-----------
//Create folder instance
localRepositoryDirectory = new File(localRepositoryLocation);
//Open repository
git = Git.open(localRepositoryDirectory);
//Perform 'git add .' to stage all new/modified files
git.add()
.addFilepattern(".")
.call();
//Perform 'git commit' locally
git.commit()
.setMessage(commitMessage)
.call();
//Chain this to your local 'git' object initialized above
git.push()
.setCredentialsProvider(new UsernamePasswordCredentialsProvider("x-token-auth", token))
.call();
} catch (Exception e) {
e.printStackTrace();
}
}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
Code End
+++++++++++++++++++++++++++++++++++++++++++++++++++++++