Learn Branching with Bitbucket Cloud | Atlassian Git Tutorial (2024)

$mkdirtest-repositories$cdtest-repositories/$test-repositories

The preceding example creates the test-repositories directory using the mkdir (make directory) command and switches to that directory using the cd (change directory) command.

5. Clone the forked repository into the directory you just created. It might look something like this:

$gitclonehttps://dstevenstest@bitbucket.org/dstevenstest/mygittutorial.bitbucket.io.gitCloninginto'mygittutorial.bitbucket.io'...remote:Countingobjects:12392,done.remote:Compressingobjects:100%(12030/12030),done.remote:Total12392(delta8044),reused564(delta360)Receivingobjects:100%(12392/12392),2.72MiB|701.00KiB/s,done.Resolvingdeltas:100%(8044/8044),done.$cdmygittutorial.bitbucket.io/

This clones the repository using thegit clonecommand and creates the directory the clone createdmygittutorial.git.bitbucket.io.

Create a branch and change something using the branching workflow

You're going to add a quote on your website in this branch.

1. Create a branch using thegit branchcommand.

$gitbranchtest-1

2.Check out the branch you just created using thegit checkoutcommand.

$gitcheckouttest-1Switchedtobranch'test-1'

3.List the branches you have locally using thegit branchcommand.

$gitbranchmain*test-1

4.Make an update to theeditme.htmlfile by adding a quote. You can use something like the following:

Thisisaquote,andIlikeit.
Aquote:TheArtofQuoting

5. Add that change.

gitaddeditme.html

Note: Your change isn't committed to the git history yet – it's in a "waiting" state. We learned about this inSaving changes.

6. Commit the change with a descriptive commit message.

gitcommiteditme.html-m'addedanewquote'[test-1063b772]addedanewquote1filechanged,3insertions(+),3deletions(-)

Note: Now the change is part of the git history as a single "commit."We learned about this inSaving changes.

7. Push that change to Bitbucket using the git push command.

gitpushfatal:Thecurrentbranchtest-1hasnoupstreambranch.Topushthecurrentbranchandsettheremoteasupstream,usegitpush--set-upstreamorigintest-1

You will see an error because the first time you push a new branch you created locally you have to designate that branch.

8. Push the branch and change using thegit push branchcommand.

$gitpushorigintest-1Countingobjects:3,done.Deltacompressionusingupto8threads.Compressingobjects:100%(3/3),done.Writingobjects:100%(3/3),363bytes|0bytes/s,done.Total3(delta2),reused0(delta0)remote:remote:Createpullrequestfortest-1:remote:https://bitbucket.org/dstevenstest/dans.git.bitbucket.org/pull-requests/new?source=test-1&t=1remote:Tohttps://bitbucket.org/dstevenstest/dans.git.bitbucket.org.git*[newbranch]test-1->test-1

This tells the system that the origin repository is the destination of this new branch.

9. Open your tutorial repository and click Branches. You should now see both the mainand the test-1 branches. It should look something like this:

Learn Branching with Bitbucket Cloud | Atlassian Git Tutorial (1)

Create, fetch, and checkout a remote branch

When you're working in a team you'll likely have to pull or fetch branches which other team members create and push to Bitbucket. This example will give you some of the basics of creating and working with branches others create.

1. Go to your tutorial repository in Bitbucket and clickBranches. You should see something like this:

Learn Branching with Bitbucket Cloud | Atlassian Git Tutorial (2)

2. ClickCreate branch, name the branchtest-2, and clickCreate.

3. Copy thegit fetchcommand in the check out your branch dialog. It will probably look something like this:

$gitfetch&&gitcheckouttest-2Fromhttps://bitbucket.org/dstevenstest/dans.git.bitbucket.org*[newbranch]test-2->origin/test-2Branchtest-2setuptotrackremotebranchtest-2fromorigin.Switchedtoanewbranch'test-2'

4. Use thegit branchcommand in your terminal. You should see a list of branches something like this:

$gitbranchmaintest-1*test-2

The branch with the asterisk * is the active branch. This is critical to remember when you are working in any branching workflow.

5. Use thegit statuscommand and you'll see something like this:

$gitstatusOnbranchtest-2Yourbranchisup-to-datewith'origin/test-2'.nothingtocommit,workingtreeclean

You can see what branch you're on and that the branch is currently up to date with your remote (origin) branch.

6. Use thegit checkoutcommand to change the focus back to your other branch. The command will look something like this:

$gitcheckouttest-1Switchedtobranch'test-1'Yourbranchisaheadof'origin/test-1'by3commits.(use"gitpush"topublishyourlocalcommits)

One of the most important things to remember when working in branches is that you want to be sure the branch you're making changes to is the correct branch.

Push change and create a pull request

Now it's time to get your first change reviewed and merge the branch.

1. Click+> Create a pull request. You can see yourtest-1branch as the source branch andmainin the destination branch.

Because we created this repository by forking an existing repository the destination is set to the mainbranch of the repository we forked.

To correct this you will need to change the repository destination branch (the branch into which you will merge your changes) from tutorials/tutorials.git.bitbucket.org to your repository.

Learn Branching with Bitbucket Cloud | Atlassian Git Tutorial (3)

You would also add reviewers on your team to the pull request. Learn more about pull requests

2. ClickCreate pull request.

3. Make a comment in the pull request by selecting a line in the diff (the area displaying the change you made to theeditme.htmlfile).

4. ClickApprovein the top left of the page. Of course in a real pull request you'd have reviewers making comments

5. ClickMerge.

6. (Optional) Update theCommit messagewith more details.

7. Select theMerge commitMerge strategyfrom the two options:

  • Merge commit—Keeps all commits from your source branch and makes them part of the destination branch. This option is the same as entering git merge --no-ff in the command line.
  • Squash—Combines your commits when you merge the source branch into the destination branch. This option is the same as entering git merge --squash in the command line.

Learn more for details on these two types of merge strategies.

8. ClickCommitsand you will see how the branch you just merged fits into the larger scheme of changes.

Delete a branch and pull main into local working branch

Now you've gone through the basic branching workflow and your change is in main. The last thing we'll learn is how to delete the branch you just merged, pull the updated mainbranch, and merge the updated mainbranch into yourtest-2branch.

Why delete the branch?

Remember, branching in Git differs from SVN or similar version control systems by using a branches as both long running branches, like a mainand development branch, and short term development branches like the examples we use in this tutorial. Because this is the case it's not a bad idea to delete local branches to keep your local environment cleaner.

Why pull mainand merge it into test-2?

We're using this as an example of you working on a repository into which another team member is working. It's a good idea to pull changes into your working branch from time to time to prevent merge conflicts in pull requests.

1. Open your terminal and run thegit statuscommand. The result should look something like this:

$gitstatusOnbranchtest-1nothingtocommit,workingtreeclean

You can see you're on the branch you just used to make your change and that you don't have any changes. We're ready to get rid of that branch now that we've finished that work.

2. Switch to the mainbranch by running thegit checkout maincommand. The result should look something like this:

gitcheckoutmainSwitchedtobranch'main'Yourbranchisup-to-datewith'origin/main'.

Notice that the message says you are up-to-date? This is only your local branch. We know this because we just merged a change into mainand haven't pulled that change from the remote repository to our local system. That's what we'll do next.

3. Run thegit pullcommand. The result should look something like this:

$gitpullremote:Countingobjects:1,done.remote:Total1(delta0),reused0(delta0)Unpackingobjects:100%(1/1),done.Fromhttps://bitbucket.org/dstevenstest/dans.git.bitbucket.org2d4c0ab..dd424cbmain->origin/mainUpdating2d4c0ab..dd424cbFast-forwardeditme.html|6+++---1filechanged,3insertions(+),3deletions(-)

What happened is that when you pull the changes from the remote repository git runs a fast-forward merge to integrate the changes you made. It also lists how many files and lines in that file changed.

4. Run thegit branch -d {branch_name}command to remove thetest-1branch. The result will look something like this:

$gitbranch-dtest-1Deletedbranchtest-1(was063b772)

You can see that it deleted the branch and what the last commit hash was for that branch. This is the safe way to delete a branch because git won't allow you to delete the branch if it has uncommitted changes. You should be aware however that this won't prevent deleting changes which are committed to the git history but not merged into another branch.

5. Switch to thetest-2branch using thegit checkoutcommand.

$gitcheckouttest-2Switchedtobranch'test-2'Yourbranchisup-to-datewith'origin/test-2'.

6.Merge the mainbranch into your working branch using thegit merge maintest-2command. The result will look something like this:

$gitmergemaintest-2Updating2d4c0ab..dd424cbFast-forwardeditme.html|6+++---1filechanged,3insertions(+),3deletions(-)

It's important to remember the following:

  • The active branch matters. If you want to merge maininto test-2, you need to have test-2 checked out (active). The same is true if you want to merge test-2 into main– you need to have mainchecked out.
  • To see what branch is active at any time, usegit branchand the active branch will have an asterisk. Or usegit statusand it will tell you want branch you are on and if there are pending local changes.

We hope you've learned a bit about branching and the commands involved. Let's review what we just covered:

Review the branching workflow

The Git Feature Branch workflowis an efficient way to get working with your team in Bitbucket.In this workflow, all feature development takes place on branches separate from the mainbranch. As a result, multiple developers can work on their own features without touching the main code.

Start with themainbranch

This workflow helps you collaborate on your code with at least one other person. As long as your Bitbucket and local repos are up-to-date, you're ready to get started.

Create anew-branch

Use a separate branch for each feature or issue you work on. After creating a branch, check it out locally so that any changes you make will be on that branch.

Update, add, commit, and push changes

Work on the feature and make commits like you would any time you use Git. When ready, push your commits, updating the feature branch on Bitbucket.

Get your code reviewed

To get feedback on your code, create a pull request in Bitbucket. From there, you can add reviewers and make sure everything is good to go before merging.

Resolve feedback

Now your teammates comment and approve. Resolve their comments locally, commit, and push changes to Bitbucket. Your updates appear in the pull request.

Merge your branch

Before you merge, you may have to resolve merge conflicts if others have made changes to the repo. When your pull request is approved and conflict-free, you can add your code to the mainbranch. Merge from the pull request in Bitbucket.

This tutorial is limited in its ability to show how branches make teams more effective. There are several approaches to branching and we discuss some of these approaches in:Comparing workflows.

Learn Branching with Bitbucket Cloud | Atlassian Git Tutorial (2024)

FAQs

What are the three types of branching in Git? ›

There are three types of supporting branches with different intended purposes: feature, release, and hotfix.

How to work with Bitbucket and Git? ›

Bitbucket Support
  1. Tutorial: Learn Bitbucket with Git. Create a Git repository. Copy your Git repository and add files. Pull changes from your Git repository on Bitbucket Cloud. Use a Git branch to merge a file.
  2. Tutorial: Learn Bitbucket with Sourcetree.
  3. Tutorial: Learn about Bitbucket pull requests.

How to create a new branch in Bitbucket using git bash? ›

If you want to create a Git branch, the best way to do it is from Bitbucket.
  1. From the repository, select the Create button.
  2. Select Branch under the This repository section of the dropdown menu.
  3. From the popup that appears, select a Type (if using the Branching model), enter a Branch name and click Create.

What is the best branching strategy in Git? ›

Keep your branch strategy simple
  • Use feature branches for all new features and bug fixes.
  • Merge feature branches into the main branch using pull requests.
  • Keep a high quality, up-to-date main branch.
Mar 25, 2024

Can we merge two branches in Bitbucket? ›

If a feature branch is behind the current branch, you can sync (merge) the branch with the mainline. You sync a branch, for example, to catch your branch up to all the latest changes on the mainline.

What is the main purpose of Git branching? ›

Git branches are effectively a pointer to a snapshot of your changes. When you want to add a new feature or fix a bug—no matter how big or how small—you spawn a new branch to encapsulate your changes.

What is the difference between branch and repository in Git? ›

In the first lesson, we defined a branch as a set of files that changes over time. A repository can have several branches at the same time and each one is maintained with its own record of commits over time.

What is the difference between main branch and master branch? ›

They have the same exact commit. Main is the new default branch, master is there to handle breakage in downstream project and permit an easy migration. Master is scheduled to be dropped after a transition period still to be defined.

What is Bitbucket Cloud? ›

Bitbucket Cloud is a Git based code hosting and collaboration tool, built for teams. Bitbucket's best-in-class Jira and Trello integrations are designed to bring the entire software team together to execute on a project.

How do I start working with Bitbucket? ›

How to Use Bitbucket
  1. Install Git. Git is an open-source software package that helps developers manage the various versions of their coding projects. ...
  2. Sign Up for a Bitbucket Account. ...
  3. Create Your First Repository. ...
  4. Clone the Repository to the Local System. ...
  5. Commit a File to Your Main Repository.

What is the difference between Git and Bitbucket? ›

The main difference between Git and Bitbucket is that Git is a distributed version control system, while Bitbucket is a web-based version control repository hosting service for development projects that use Git or Mercurial.

What is a branch in Bitbucket? ›

Branching offers a way to work on a new feature without affecting the main codebase. You can create a branch from Bitbucket, Jira, or from your terminal. After you make changes, you push your branch to Bitbucket so that you can get it reviewed with a pull request.

How do I push code into the Bitbucket branch? ›

Push code to Bitbucket
  1. Create your new files or edit existing files in your local project directory.
  2. From the command line, enter cd <path_to_local_repo> so that you can enter commands for your repository.
  3. Enter git add --all at the command line to add the files or changes to the repository.

How can I tell when a branch was created in Bitbucket? ›

If you have admin on the repo, you can go to the Push Log and choose the branch name to get the push history. The first entry is the creation date in bitbcket. Note that this might be different than the actual date the branch was created locally which can be retrieved using git commands.

How do I give access to a specific branch in Bitbucket? ›

On the Branch permissions tab under Write access, select Only specific people or groups have write access: Alana Persona. On the Branch permissions tab under Merge access via pull requests, select Only specific people or groups have merge access: Harvey Persona.

What is branching strategy in Bitbucket? ›

The branching strategy is one of the features which is provided by Bitbucket. The center thought behind the Feature Branch Workflow is that all included advancement should occur in a devoted branch rather than the default.

How do I edit a branch in Bitbucket? ›

In Bitbucket Cloud, please go to [Your Repository] >> Settings >> General >> Repository details >> Update repository details >> Main branch.

How do I push code from one branch to another in Bitbucket? ›

Push code to Bitbucket
  1. Create your new files or edit existing files in your local project directory.
  2. From the command line, enter cd <path_to_local_repo> so that you can enter commands for your repository.
  3. Enter git add --all at the command line to add the files or changes to the repository.

References

Top Articles
Latest Posts
Article information

Author: Merrill Bechtelar CPA

Last Updated:

Views: 6092

Rating: 5 / 5 (50 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Merrill Bechtelar CPA

Birthday: 1996-05-19

Address: Apt. 114 873 White Lodge, Libbyfurt, CA 93006

Phone: +5983010455207

Job: Legacy Representative

Hobby: Blacksmithing, Urban exploration, Sudoku, Slacklining, Creative writing, Community, Letterboxing

Introduction: My name is Merrill Bechtelar CPA, I am a clean, agreeable, glorious, magnificent, witty, enchanting, comfortable person who loves writing and wants to share my knowledge and understanding with you.