Back to Git & Dev Tools
2026-02-177 min read

Learn branching in Bitbucket Cloud

Learn Learn branching in Bitbucket Cloud step by step with clear examples and exercises.

Title: Mastering Branching with Bitbucket Cloud - A full guide

Learn Branching with Bitbucket Cloud is an indispensable skill for developers working on collaborative projects, enabling them to manage changes and conflicts effectively. In this tutorial, we will delve deeper into branching in Bitbucket Cloud, explaining its importance, prerequisites, core concept, worked example, common mistakes, practice questions, and frequently asked questions.

Why This Matters

Branching is a fundamental Git feature that allows developers to work on separate parts of a project without affecting the main codebase. In collaborative projects, branching helps team members experiment with new features or fix bugs independently, reducing conflicts and ensuring a smoother development process. Bitbucket Cloud offers an intuitive interface for managing branches, making it a popular choice for developers working on large-scale projects.

Benefits of Branching

  1. Isolate changes: Developers can work on new features or bug fixes without affecting the main codebase.
  2. Collaborate efficiently: Team members can collaborate more effectively by focusing on their assigned tasks in separate branches.
  3. Reduce conflicts: By isolating changes, developers can minimize merge conflicts and streamline the merging process.
  4. Iterative development: Branching allows for iterative development, where developers can test and refine their work before merging it into the main codebase.
  5. Code reviews: Branches facilitate code reviews by providing a clear separation between completed and in-progress work.

Prerequisites

Before diving into branching in Bitbucket Cloud, you should have a good understanding of Git and its commands, as well as familiarity with the Bitbucket Cloud interface. You should also be comfortable with creating repositories, committing changes, and merging branches in Git. If you're new to Git, consider checking out our previous tutorial on Git basics before proceeding.

Essential Git Commands

  1. git init: Initialize a local Git repository.
  2. git clone: Clone an existing remote repository onto your machine.
  3. git add: Stage files for commit.
  4. git commit: Commit staged changes to the local repository.
  5. git push: Push commits from the local repository to a remote repository.
  6. git pull: Pull updates from a remote repository into the local repository.
  7. git merge: Merge one branch into another.
  8. git branch: List, create, and manage branches.
  9. git checkout: Switch between branches or files within a branch.
  10. git status: Display the current state of the working directory and staging area.

Core Concept

Branching in Bitbucket Cloud is based on the Git branching model. To create a new branch, follow these steps:

  1. Navigate to your repository in Bitbucket Cloud and click on the "Branches" tab.
  2. Click the "Create branch" button and enter a name for your new branch (e.g., feature/new-feature).
  3. Select the base branch for your new branch (usually master or main).
  4. Click "Create."

Now you can make changes in your new branch without affecting the main codebase. When you're ready to merge your changes, follow these steps:

  1. Switch to the branch containing the changes by clicking on it in the "Branches" tab.
  2. Make sure your local repository is up-to-date with the remote repository using the following commands:
git pull origin master
git checkout <your-branch>
  1. Commit your changes and push them to Bitbucket Cloud:
git add .
git commit -m "Your commit message"
git push origin <your-branch>
  1. Navigate back to the main branch in Bitbucket Cloud, and click on the "Merge" button next to your new branch. Confirm the merge request, and your changes will be merged into the main codebase.

Branch Naming Conventions

  1. Feature branches: Start with feature/ followed by a brief description of the feature (e.g., feature/new-feature).
  2. Bug fix branches: Start with fix/ followed by a brief description of the issue being addressed (e.g., fix/login-page-bug).
  3. Hotfix branches: Start with hotfix/ followed by a brief description of the fix (e.g., hotfix/urgent-security-patch).
  4. Release branches: Start with release/ followed by a version number or release name (e.g., release/v1.0.0).

Worked Example

Let's work through an example of creating a new feature branch, making changes, and merging them back into the main branch.

  1. Create a new branch called feature/new-feature based on the master branch:
git checkout master
git checkout -b feature/new-feature
  1. Make changes to a file, such as adding a new function or modifying an existing one. For example, let's add a new function called say_hello to the app.js file:
// app.js
function say_hello() {
console.log("Hello, world!");
}
  1. Commit your changes and push them to Bitbucket Cloud:
git add .
git commit -m "Add new say_hello function"
git push origin feature/new-feature
  1. Navigate back to the main branch in Bitbucket Cloud, and click on the "Merge" button next to your feature/new-feature branch. Confirm the merge request, and your changes will be merged into the main codebase.

Merge Strategies

Bitbucket Cloud supports various merge strategies, such as:

  1. Fast-forward: If there are no conflicting changes between branches, Bitbucket Cloud automatically merges the branch using a fast-forward strategy.
  2. Merge commit: When there are conflicting changes between branches, Bitbucket Cloud creates a new merge commit that combines both sets of changes. Developers can then resolve any conflicts manually.
  3. Rebase: Rebasing allows developers to integrate changes from another branch into their current branch while maintaining a linear history. This can be useful for keeping the project's commit history clean and organized.

Common Mistakes

  1. ### Forgetting to create a new branch

Always create a new branch before making changes to avoid affecting the main codebase.

  1. ### Committing unfinished work

Only commit changes that are complete and ready for review. Unfinished work can cause confusion and conflicts when merging branches.

  1. ### Not updating your local repository before merging

Always ensure your local repository is up-to-date with the remote repository before merging branches to avoid conflicts.

  1. ### Ignoring merge conflicts

When merge conflicts occur, resolve them promptly to prevent further issues down the line.

  1. ### Merging multiple feature branches at once

Merging multiple feature branches simultaneously can lead to complex merge conflicts and make it difficult to identify and resolve individual issues. Instead, merge each branch individually.

  1. ### Not using descriptive branch names

Using clear and descriptive branch names helps other developers understand the purpose of each branch and makes it easier to track changes throughout the project.

Practice Questions

  1. You want to add a new feature called feature/new-page based on the master branch. What commands would you use?
git checkout master
git checkout -b feature/new-page
  1. You've made changes in your feature/new-feature branch, but there are merge conflicts with the main codebase. How can you resolve these conflicts?

Resolve conflicts by manually editing the affected files and committing the changes. Then, push the updated branch to Bitbucket Cloud for merging.

  1. You've created a new feature branch called feature/new-page, but you realize that it's based on an outdated version of the main codebase. How can you switch your new branch to the latest version of the main codebase?

To switch your new branch to the latest version of the main codebase, first fetch and merge the updates from the remote repository:

git checkout feature/new-page
git pull origin master
git merge master

FAQ

### Can I delete a branch once it's merged into the main codebase?

Yes, you can safely delete a branch after it has been merged and its changes have been incorporated into the main codebase. However, it's a good practice to keep the branch around for a short period in case any issues arise that require reverting the changes.

### What happens if I merge a branch with uncommitted changes?

If you try to merge a branch with uncommitted changes, Git will prevent you from merging until you commit or discard those changes.

### Can I revert changes made in a specific branch?

Yes, you can revert changes made in a specific branch by creating a new merge commit that undoes the changes. This is known as a "revert" commit. To create a revert commit, first find the commit hash of the change you want to revert and use it in the following command:

git revert <commit-hash>

### How can I view the history of a specific branch?

To view the history of a specific branch, navigate to your repository in Bitbucket Cloud and click on the "Branches" tab. Click on the branch you want to inspect, and then click the "Files" or "Commits" tab to view its history.

### Can I merge branches using the command line instead of the Bitbucket Cloud interface?

Yes, you can merge branches using Git commands from the command line. To do this, navigate to your local repository and use the git checkout and git merge commands as described in the Core Concept section.

Learn branching in Bitbucket Cloud | Git & Dev Tools | XQA Learn