目的

Start a repository with someone else and get some feedback about your change.

ミッションの概要

So far, you've been the only person working in a repository. But what if you wanted to collaborate with your teammates on a repository? You can do that, whether you're in the same room or across the universe.

所要時間 対象者 前提条件
15 分 You have some experience using Bitbucket Cloud. If not, try out one of our beginner tutorials. Bitbucket Cloud にサインアップ
なし For those who prefer using a Git GUI with Bitbucket Cloud Sign up for Sourcetree

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.

リポジトリを作成してチームメイトを追加する

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. To get you started, we'll walk you through creating a team repository with some content and giving someone access.

Step 1. Create a team and add a teammate

Start by creating a team for your repository and teammate. No need to have a teammate for this tutorial. For our purposes, you'll make a new friend that goes by the username breezycloud.

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

  2. Enter a Team name you'd like to use. If you can't think of any, we suggest starting with Planet followed by a number or name, for example Planet Breezy Cloud or Planet 727. If you pick a team name that already exists, you may need to edit the Team ID because all IDs must be unique.

    Git ブランチのフォーク

  3. Click Done.

  4. You'll land on your team Repositories page. From there, click Settings.

  5. Click User groups under Access management. You'll see that you have only two groups and you're the only member of both groups.

  6. Click the Developers group. From there, you'll see a place to add new members.

  7. Search for breezycloud in the Username or email address field, pick that user, and click Add.

Now when you create a pull request for your future repository, you'll have someone to review it!

Step 2. Create a repository with some content

No worries about putting a whole bunch of code together for this repository. We'll provide you with some to start.

  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.

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style media="screen" type="text/css">
body {
margin: auto;
width: 700px;
color: #FFFFFF;
font-family: Arial, sans-serif;
background-color: #172B4D;
}
body>h1 {
margin: 50px;
font-size: 50px;
text-align: center;
color: #0052CC;
}
</style>
</head>
<body>
<h1>Team up in space</h1>
<p>
Welcome to the team! You've made it this far so we know that you've got the potential to do great things. Because you're going to be collaborating with other awesome people, anything you add needs to be code reviewed and approved. That's just how a team works! You should have already created a branch and checked it out locally. If you haven't, go back to the tutorial and do that now. We'll be here.
</p>
<p>
Because you're on your own branch, you can go crazy. Spice up this file any way you like. Add more files to this repository if you see fit. If want to take it slow and are just here to learn about pull requests, you can use this opportunity to fill out our short questionaire.
</p>
<br>
<p>
<b>Question 1</b>: Have you used pull requests before?
</p>
<p>
<b>Answer 1</b>: **** Your answer here ****
</p>
<p>
<b>Question 2</b>: Why do you want to learn about code review?
</p>
<p>
<b>Answer 2</b>: **** Your answer here ****
</p>
<p>
<b>Question 3</b>: Who do you plan to work with on Bitbucket?
</p>
<p>
<b>Answer 3</b>: **** Your answer here ****
</p>
</body>
</html>
  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. If you click Settings and go to the User and groups access page, you'll see that your breezycloud teammate you added has repository access through the Developers group.

クローンを作成して新しいブランチで変更を行う

In a typical team context, 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.

コマンドラインを使っている場合

ステップ 1. ローカルシステム上でリポジトリをクローンする

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>
  1. Paste the command you copied from Bitbucket, for example:
$ git clone https://breezy@bitbucket.org/powerstars/first-impressions.git

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.

For more details, check out our cloning video to see how it's 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

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
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")
  1. Add your changes locally with git add <filename>:
$ git add survey.html
  1. Commit your changes locally with  git commit -m "your commit message":
$ git commit -m "Answered questions"
[my-updates 7506040] Answered questions
 1 file changed, 3 insertions(+), 3 deletions(-)
  1. 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

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.
  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.

Sourcetree を使っている場合

ステップ 1. ローカルシステム上でリポジトリをクローンする

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 <path_to_repo_directory>/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.

Create a pull request to merge your change

To alert your teammates to your updates and get their approval, 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.

    3. From the Reviewers field, add your team member breezycloud as a reviewer.

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

Bitbucket opens the pull request and your reviewer receives an email notification with details of 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.