Clone and make a change on a new branch

When you know that you will be adding reviewers to review and approve your code prior to merging, you’d most likely already have the repository cloned before creating a branch. So that’s what we’re going to do first before you set up your own branch.

I'm using Git!
Step 1. Clone your repository to your local system

Let's get it onto your local system so that you can really start working on it.

  1. From the repository, click the Clone button in the top right.
    Bitbucket displays the Clone this repository dialog. By default, the clone dialog sets the protocol to HTTPS or SSH, depending on your settings. As a result, you don't need to change your default protocol.

  2. Copy the clone command.

  3. From a terminal window, change into the local directory where you want to clone your repository.
    $ cd ~/<path_to_directory>

  4. Paste the command you copied from Bitbucket, for example:
    $ git clone  https://breezy@bitbucket.org/powerstars/first-impressions.git

    1 2 3 4 5 6 Cloning into 'first-impressions'... Password for 'https://breezycloud@bitbucket.org': remote: Counting objects: 6, done. remote: Compressing objects: 100% (5/5), done. remote: Total 6 (delta 1), reused 0 (delta 0) Unpacking objects: 100% (6/6), done.

Step 2. Create a branch and pull in locally

Now that your repository is all set up, next comes the fun part. You can create branches locally or through Bitbucket. Let's create one from Bitbucket for the purposes of this tutorial.

  1. Click Branches from the left navigation. You'll see that you already have one branch — your main branch, master.

  2. Click Create a branch in the top right corner.

  3. After you create a branch, you need to check it out on your local system. Bitbucket provides you with a fetch and checkout command that you can copy and paste into your command line, similar to the following:
    $ git fetch && git checkout my-updates

    1 2 3 4 5 6 Password for 'https://breezycloud@bitbucket.org': From https://bitbucket.org/planetbreezycloud/first-impressions * [new branch] my-updates -> origin/my-updates Branch 'my-updates' set up to track remote branch 'my-updates' from 'origin'. Switched to a new branch 'my-updates'

As you can see, you've switched to your new branch locally, allowing you to work on and push that separate line of code.

Step 3. Make a change to the branch

Now, it's your turn to makes some changes to your repository. Like the file mentions, you can go as crazy or as simple as you like. Change up the CSS. Add more files. Compose a space opera. Or simply answer the questions.

  1. Open the survey.html file (or whatever you named it) with a text editor.

  2. Make your changes, big or small, and then save and close the file.

  3. From your terminal window, you should still be in the repository directory unless you've changed something.  Display the status of the repository with  git status . You should see the survey.html file you modified. If you added or modified other files, you'll see those as well.

    $ git status

    1 2 3 4 5 6 7 On branch my-updates Your branch is up-to-date with 'origin/my-updates'. Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: survey.html no changes added to commit (use "git add" and/or "git commit -a")
  4. Add your changes locally with git add <filename>:
    $ git add survey.html

  5. Commit your changes locally with  git commit -m "your commit message":

    1 2 3 $ git commit -m "Answered questions" [my-updates 7506040] Answered questions  1 file changed, 3 insertions(+), 3 deletions(-)
  6. Enter git push origin <branch_name> to push the changes to your branch on Bitbucket, and enter your password to finish pushing changes.
    $ git push origin my-updates

    1 2 3 4 5 6 7 8 9 Password for 'https://breezycloud@bitbucket.org': Counting objects: 3, done. Delta compression using up to 8 threads. Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 350 bytes | 350.00 KiB/s, done. Total 3 (delta 1), reused 0 (delta 0) To https://bitbucket.org/planetbreezycloud/first-impressions.git 454ccaf..7506040 my-updates -> my-updates Branch master set up to track remote branch master from origin.
  7. From Bitbucket, click the Source page of your repository. You should see both branches in the dropdown. Any other commits you make to my-updates will also appear on that branch.


I'm using Sourcetree!
Step 1. Clone your repository to your local system

Let's get it onto your local system so that you can really start working on it.

  1. From the repository, click the Clone button in the top right.

  2. Click the Clone in Sourcetree button.

  3. From the Clone New window, update the Destination Path to <path_to_repo_directory>/first-impressions/.

clone a branch in Sourcetree

4. Click the Clone button.

Step 2. Create a branch and pull in locally

Now that your repository is all set up, next comes the fun part. Because branches aren't only a Bitbucket concept, you can create one locally. However, we're going to create one from Bitbucket for the purposes of this tutorial.

  1. Click Branches from the left navigation. You'll see that you already have one branch — your main branch.

  2. Click Create a branch in the top right corner.

  3. Enter a Branch name and click Create. If you aren't sure what to name your branch, go with something like my-updates.

  4. After you create a branch, you need to check it out from your local system. To do so, click the Check out in Sourcetree button.

  5. From the Checkout Existing dialog in Sourcetree, click Checkout.

Now you've got a branch in Bitbucket and it's checked out to your local system, allowing you to work on and push that separate line of code.

Step 3. Make a change to the branch

Now, it's your turn to makes some changes to your repository. Like the file mentions, you can go as crazy or as simple as you like. Change up the CSS. Add more files. Compose a space opera. Or simply answer the questions.

  1. From the repository in Sourcetree, click the Show in Finder button.

  2. Open the survey.html file (or whatever you named it) with a text editor.

  3. Make your changes, big or small, and then save and close the file.

  4. Open Sourcetree and notice that your repository has Uncommitted changes.

  5. Add the file to the staging area:

    1. Select the Uncommitted changes line.

    2. From the Unstaged files list, place a checkmark next to the survey.html file (and any other files with uncommitted changes).

    3. From the Confirm Stage? dialog, click OK.

  6. Click the Commit button at the top to commit the file.

  7. Enter a commit message in the space provided, something like Answered questions.

  8. Click the Commit button under the message box. When you switch back to the view, you see that the file has been committed but not pushed to the Bitbucket repository.

  9. From Sourcetree, click the Push button to push your committed changes.

  10. From the dialog that appears, click OK to push your branch with the commit to Bitbucket.

  11. From Bitbucket, click the Source page of your repository. You should see both branches in the dropdown. Any other commits you make to my-updates will also appear on that branch.

Next

Still need help?

The Atlassian Community is here for you.