The Atlassian Community Forums are currently in read-only mode. We will be relaunching on a new platform on September 22 (read more here). We apologize for the extended downtime. For concerns or questions, please email communitymanagers@atlassian.com. See you on the other side, on the new Atlassian Community Forums! :)

×

Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Solution: Use JGit to perform clone, add, commit, push Git operations with Bitbucket access tokens

Martin Svedlow
July 22, 2026

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:

  • git clone https://x-token-auth:<TOKEN>@bitbucket.org/username/repositorylowercase.git

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

+++++++++++++++++++++++++++++++++++++++++++++++++++++++

 

 

 

1 answer

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

0 votes
Aditya Srivastav
Contributor
August 26, 2026

Hi Martin,

Thanks for sharing the JGit example. The main thing to keep in mind is that Bitbucket Cloud has different token types, and the authentication format depends on which one you created.

For a Bitbucket repository/project/workspace access token, x-token-auth is the correct username when using HTTPS, for example:

https://x-token-auth:<ACCESS_TOKEN>@bitbucket.org/<workspace>/<repository>.git

Atlassian documents this format for cloning, and the token needs the appropriate repository permissions such as Repository Read for cloning and Repository Write for pushing.

For a normal Bitbucket API token, however, the authentication format is different. Atlassian documents x-bitbucket-api-token-auth (or the account's Bitbucket username) as the username, with the API token supplied as the password.

So one possible reason the command shown in the post fails is that the token type and authentication format do not match.

For JGit, your approach of using:

new UsernamePasswordCredentialsProvider("x-token-auth", token)

is appropriate when the credential is specifically a Bitbucket access token. The important part is to make sure the token has the required repository permissions and that the repository/workspace path is correct.

One other point: embedding the token directly into the remote URL works, but Atlassian recommends avoiding storing access tokens permanently in URLs because they can be exposed in Git configuration or logs. Using a credentials provider/secure secret is preferable.

So the JGit solution itself is valid; the key is matching the token type, username format, scopes/permissions, and repository URL correctly.

DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events