Bitbucket, developed by Atlassian, is a Git-based source code repository hosting service. It is designed for teams and provides strong integration with other Atlassian tools like Jira, Trello, and Confluence. This cheatsheet provides a detailed guide for mastering Bitbucket from basic operations to advanced features.
- Bitbucket is a Git-based platform for version control, CI/CD pipelines, and project collaboration.
- It supports both private and public repositories.
- Known for its seamless integration with Atlassian tools (e.g., Jira) and in-built CI/CD pipelines.
- Git repository hosting
- In-built CI/CD via Bitbucket Pipelines
- Jira integration for issue tracking
- Branch permissions and code review tools
- Supports Mercurial (deprecated)
- Go to Bitbucket and sign up for an account.
- Optionally, link your Atlassian account for better integration.
-
Generate an SSH key:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com" -
Add the public key to Bitbucket:
- Navigate to Personal Settings → SSH Keys → Add Key.
- Log in to Bitbucket.
- Go to Repositories → Create Repository.
- Configure:
- Repository Name
- Access level (Private/Public)
- Repository Type (Git)
git clone git@bitbucket.org:username/repository.git# Stage changes
git add .
# Commit changes
git commit -m "Initial commit"
# Push changes
git push origin maingit pull origin main# Create a new branch
git checkout -b feature-branch
# Switch to an existing branch
git checkout maingit push origin feature-branch- Open Bitbucket and navigate to Pull Requests.
- Click Create Pull Request.
- Select branches, add reviewers, and provide a description.
- Approve the PR.
- Merge using the options:
- Merge Commit: Keeps all commits intact.
- Squash Merge: Combines all commits into one.
- Rebase: Rewrites commit history.
Bitbucket Pipelines is an integrated CI/CD service to automate builds, tests, and deployments.
- Go to the repository settings → Pipelines.
- Enable Pipelines and configure the
.bitbucket-pipelines.ymlfile.
pipelines:
default:
- step:
name: Build and Test
image: node:14
script:
- npm install
- npm test
- step:
name: Deploy to Production
script:
- echo "Deploying to production..."- default: Runs on any branch when pushed.
- branches: Customizes triggers for specific branches.
- tags: Automates deployment for version tags.
- Go to Repository Settings → Pipelines → Environment Variables.
- Add sensitive variables like
AWS_ACCESS_KEY.
script:
- echo "Using secret: $AWS_ACCESS_KEY"- Go to Repository Settings → Branch Permissions.
- Add rules such as:
- Prevent direct pushes to
main. - Require at least 2 code reviews before merging.
- Prevent direct pushes to
- Admin: Full control over repositories and permissions.
- Write: Can push and pull code.
- Read: Read-only access to repositories.
- Go to Repository Settings → Jira Settings.
- Connect the repository to a Jira project.
-
Add Jira issue keys in commit messages:
PROJ-123: Fix login page bug -
Jira automatically links commits, pull requests, and deployments.
- Assign reviewers while creating a pull request.
- Add comments inline to highlight issues.
-
Add tools like SonarCloud or CodeClimate to your pipelines for static code analysis.
-
Example: Adding SonarCloud to Bitbucket Pipelines:
- pipe: sonarsource/sonarcloud-scan:1.4.0 variables: SONAR_TOKEN: $SONAR_TOKEN
Generate a personal access token:
- Go to Personal Settings → Access Management → Create App Password.
Use the token in API calls:
curl -u username:app_password https://api.bitbucket.org/2.0/repositories-
List repositories:
curl -X GET https://api.bitbucket.org/2.0/repositories/{username} -
Create an issue:
curl -X POST -u username:app_password \ -H "Content-Type: application/json" \ -d '{"title": "Bug in Login Page", "content": {"raw": "Description"}}' \ https://api.bitbucket.org/2.0/repositories/{username}/{repo}/issues
Track deployment environments:
- Go to Deployments → Configure environments (e.g., Dev, Staging, Prod).
Add deployment steps in .bitbucket-pipelines.yml:
pipelines:
branches:
main:
- step:
name: Deploy to Staging
deployment: staging
script:
- ./deploy.sh stagingHost multiple services in one repository:
-
Use Pipelines for individual service builds:
pipelines: default: - step: name: Build Service A script: - cd services/service-a && npm install && npm test
Mirror a repository between Bitbucket and GitHub:
git remote add bitbucket git@bitbucket.org:username/repo.git
git push bitbucket --mirror- Go to Personal Settings → Security → Enable 2FA.
Bitbucket scans for hard-coded credentials and alerts users.
Use Atlassian tools like Snyk or Dependabot to identify vulnerabilities.
-
Branch Naming Convention:
- Use prefixes like
feature/,bugfix/, andrelease/.
feature/add-login-form bugfix/fix-authentication-error - Use prefixes like
-
Commit Messages:
-
Follow a format like:
[PROJ-123] Fix bug in login functionality -
Reference Jira issues in commit messages.
-
-
Automate Everything:
- Use Pipelines for CI/CD.
- Automate linting, testing, and deployment.
-
Use Pull Request Templates:
- Add
.bitbucket/pull_request_template.mdto standardize PR descriptions.
- Add
