Using Sourcetree With Bitbucket

For the past year or so, we’ve been using Git as our version control system. My introduction to the GUIs around Git was SourceTree (although I’ve made an effort to learn the commands) but I have also used poshgit and Git Bash. Recently, we’ve started using SSH keys instead of HTTPS and I had to learn how to set up my repositories with SSH. Everywhere and everyone tells you this is straight forward and it is when the critical path works but when something is wrong, it gets more difficult. A lot of unnecessarily complex documents does not help either. So I’m going to details all the steps that I took in the hope that it could helps someone.

My setup for this task is Git (you can use the embedded git within SourceTree), SourceTree and BitBucket (previously used Google Drive to host my git repositories).

Git Essentials: Learn Git with Bitbucket and Sourcetree Git up to speed quickly with crucial technologies that will enhance your everyday workflow. Bestseller Rating: 4.5 out of 5 4.5 (4,209 ratings) 16,988 students Created by Juke Jaster. Last updated 2/2018 English English Auto Add to cart. If you do choose to use SourceTree as a GUI Git client, you'll need to add your BitBucket credentials to SourceTree. Let's do that now. Open the SourceTree app.

  • Open SourceTree and click on the Terminal icon (this is Git Bash)
  • Type the following command in
    • ls –all ~/.ssh (this will list any existing ssh keys in C:Users.ssh, this is the default but can be changed when generating the key).
  • Next, generate the key
    • ssh-keygen –t rsa –b 4096 –C
    • It will ask you where you’d like to store the files, I accepted the default but you can specify a directory if you wish.
    • Then enter a passphrase, I would recommend you provide a passphrase from a security standpoint.
    • You should now see this this:
  • There should be two key files id_rsa (private) and id_rsa.pub now created.
  • Still using the terminal (Git Bash) in SourceTree, type:
    • eval $(ssh-agent). There are many ways to start the SSH agent but this is only way it would work for me. It should give you a process id back, something like, Agent pid 1234
  • Finally using this command to add the new key
    • ssh-add ~/.ssh/id_rsa
    • If successful, the output should say that an identity has been created.
    • You should never have to type in the passphrase again.
  • Log into BitBucket
  • Select the icon on the top right of the browser and select Manage Account
  • From the Security menu, select SSH Key then Add Key
  • Add you public key (id_rsa.pub) to the text area and then Add Key again
Using Sourcetree With Bitbucket

Note, your public key in this file is in a different format from what BitBucket expects. My recommendation for this scenario is to go to SourceTree – Tools – Create or Import SSH Keys. This starts a Putty Generator that has the ability to load existing keys. The generator will then show the public key in a user friendly format to be copied and used within BitBucket.

In Stage 1, the SSH key was generated and set up for the Git Bash terminal, now we want to take that SSH key and use it within the SourceTree GUI.

Bitbucket
  • First step is to go to Tools – Create or Import SSH Key
  • Load your existing private key in.
  • Click on “Save Private Key”. This has to be saved in the Putty .ppk format. I would recommend that you didn’t save this private key to the .ssh folder in case of conflicts between two keys.
  • Next is to launch the SSH agent – Putty comes with SourceTree.
  • Make sure Pagent is running ( little computer with a hat on sitting in your windows tray).
  • Add the key to the SSH agent by right clicking on Putty Pagent and selecting “Add Key”. It is Pagent that stops the user from entering the passphrase all the time by holding key and making it available to SourceTree.
  • A further step is to add the .ppk key to Tools – Options – General – SSH Client Configuration.

That’s it! I was all around the houses trying to fix various errors and configure. Some of the problems I faced were:

  • Permission denied (public key). I believe it was a combination of errors on my part. One, I had created too many key files in the .ssh directory and it didn’t know what one to choose. Second, I hadn’t set up SourceTree correctly. The SSH key had to be a .ppk key and not the id_rsa key, which I’d generated.
  • Could not open a connection to your authentication agent. I believe this was down to me changing from Putty to OpenSSH. OpenSSH just never launched, no wonder it couldn’t get a connection.
  • It took ages to clone a repository. SourceTree GUI doesn’t give a lot of feedback with what is going on, not like Git Bash. I thought it wasn’t working.

My tip would be to test the connection using “ssh –T [email protected]”. This command with provide decent feedback if you have or haven’t authenticated. So open Git Bash and type this in.

A good topic for debate is why go to all the trouble of using SSH keys? Why not, use HTTPS and cache you account details in winstore?

Update:

Discovered this morning that if you shut SourceTree down, if you use the Git Bash terminal, you will need to repeat Stage 2.

References

Objective

Create a repository and add someone as a reviewer to your pull request to start collaborating on your code.

Disclaimer: The following section of this tutorial outlines the steps of adding a reviewer (or team member) to your pull request, so you can collaborate on your code in Bitbucket Cloud. If you are not working with a teammate or do not need to add reviewers at this time, feel free to move onto the next tutorial: Learn branching in Bitbucket Cloud.

Mission Brief

Using

So far, you’ve been the only person working in your repositories and on your code. What if you wanted to have someone review your code and provide feedback? By adding a reviewer to your pull request, you can do just that, whether you’re in the same room or across the universe.

TimeAudiencePrerequisites
15 minutesYou have some experience using Bitbucket Cloud. If not, try out one of our beginner tutorials.
NoneFor those who prefer using a Git GUI with Bitbucket Cloud

About branches and pull requests

When you work on a team with multiple Bitbucket users, you'll want to work on your own set of code separately from the main codebase. Branches allow you to do just that. A branch represents an independent line of development for your repository. Think of it as a brand-new working directory, staging area, and project history. After you create a branch, you work on and commit code to that branch, pull updates from Bitbucket to keep your branch up-to-date, and then push all your work to Bitbucket.

Once you've got code changes on a branch in Bitbucket, you can create a pull request, which is where code review takes place. Your teammates will comment on your code with feedback and questions and eventually (hopefully) approve the pull request. When you have enough approvals, merge the pull request to merge your branch into the main code.

Bitbucket

Create a repository

You just arrived at the Bitbucket space station and it's time to go through the orientation process, part of which involves making updates to your welcome package and getting them approved.

  1. Click + in the global sidebar and select Repository under Create.

  2. Make sure the team you created is the repository Owner.

  3. Enter anything you want for the Project name and Repository name. If you can't think of anything, how about Welcome package and First impressions, respectively.

  4. From Include a README?, select either of the Yes options.

  5. From Version control system, pick an option for the type of repository you want to create. If you're not sure, keep as is.

  6. Click Create repository and you'll land on the Source view of your brand, new repository.

  7. From Source, select >Add file.

  8. Name the file survey.html, then copy this code and paste it into the main text area.

  1. Click Commit and then Commit again from the dialog.

Your repository is looking pretty good now. Take a look around if you feel like it.

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.

If you're using command line

Step 1. Clone your repository to your local system

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

Using Sourcetree With Bitbucket

  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.

  1. Paste the command you copied from Bitbucket, for example:

For more details, check out our cloning video to see how it's done:

Step 2. Create a branch and pull in locally

Sourcetree Bitbucket Server

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:

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.

  1. Add your changes locally with git add :
  1. Commit your changes locally with git commit -m 'your commit message':

Bitbucket Sourcetree Download

  1. Enter git push origin to push the changes to your branch on Bitbucket, and enter your password to finish pushing changes.
  1. 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.

If you're 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 + in the global sidebar and select Clone this repository under Get to work.
  2. Click the Clone in Sourcetree button.
  3. From the Clone New window, update the Destination Path to/first-impressions/.
  4. Click the Clone button.

To see how it's done, check out the Clone a repository part of our video here:

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. (Git only) 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.

Bitbucket sourcetree download

Create a pull request to merge your change

To add reviewers to check and approve your code prior to merging, your next step is to create a pull request. In addition to a place for code review, a pull request shows a comparison of your changes against the original repository (also known as a diff) and provides an easy way to merge code when ready.

Step 1. Create the pull request

You need a branch to create a pull request. Good thing you already have one.

  1. From your repository, click + in the global sidebar. Then, click Create a pull request under Get to work.
    Bitbucket displays the request form.
  2. Complete the form:
    1. You've already got an automatic Title, your last commit message.

    2. Add a Description if you'd like.

      (Optional - adding a reviewer) If you need someone to review and/or approve your code prior to merging, add them in the Reviewers field. You can see that Breezy Cloud was added as the reviewer to the example pull request below.

      In order to add a reviewer to a pull request, you must first grant the user access to your repository by adding them to a group. For more information and detailed steps, refer to the following: Grant repository access to users and groups. Note: You can always create the pull request and then add reviewers later.

    When you're done, the form will look something like this:
  3. Click Create pull request.

Bitbucket opens the pull request, and if you added a reviewer, they will receive an email notification with details about the pull request for them to review.

Step 2. Merge your pull request

Not so fast! You may have noticed the Merge button at the top. Before you click it, you need to wait for an approval of your changes. In addition to the email notification your teammates receive, they'll also see the pull request appear under Pull requests to review on the Your work dashboard.

From the pull request, the reviewer can view the diff and add comments to start a discussion before clicking the Approve button.

When someone approves your pull request, you'll get an email notification. Once you've got the approvals you need (in this case just one!), you can merge. From the pull request, click Merge. And that's it! If you want to see what it looks like when your branch merges with the main branch, click Commits to see the commit tree.