How to construct ‘git clone’ command to clone only a specific tag or a branch in a repository
Platform Notice: Cloud Only - This article only applies to Atlassian products on the cloud platform.
Summary
Customers encounter access errors while cloning a repo or branch
git@bitbucket.org: Permission denied (publickey). fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.
Cause
Using incorrect URLs either to clone the repo or a specific branch
Example:
URL: git clone ssh://git@altssh.bitbucket.org:443/<workspace_ID>/<Repository_SLUG>@v3.3.1
Solution
Below are the commands to clone repo/branch/tag without experiencing permission denied errors
Command to clone a repository :
git clone https://<User_Name>@bitbucket.org/<Workspace_ID>/<Repo_Name>.git
Command to clone a branch :
git clone https://<User_Name>@bitbucket.org/<Workspace_ID>/<Repo_Name>.git --branch <branch_name>
Command to clone a tag :
git clone https://<User_Name>@bitbucket.org/<Workspace_ID>/<Repo_Name>.git --branch <tag_name>
In order to clone the history of the branch or tip of a tag we can use “--single-branch“ option
Example:
git clone https://<User_Name>@bitbucket.org/<Workspace_ID>/<Repo_Name>.git --branch <branch_name> --single-branch
git clone https://<User_Name>@bitbucket.org/<Workspace_ID>/<Repo_Name>.git --branch <tag_name> –single-branch