Using Source Control with BitBucket, Source Tree and Visual Studio – Part 2

In part 1 we looked at installing our source control tools and setting up our first repository. To truly make source control as powerful as it can be we need to create new branches for various changes that we make so that we can switch back and forth between different versions of our site. In part two below we are going to look at how to do just that.

Creating a New Branch

  • Open SourceTree
  • Double click the project that you want to branch in the menu on the left or if it is currently open make sure that the project tab at the top is the selected tab
  • Click the Branch button on the menu options at the top
  • Name your new branch. Adding a classification prefix can help to organize your branches
    • feature-[branch name] – the feature prefix is useful for completely new features that you are integrating into the project such as adding a new level to a game or a new section to a web application.
      • feature-ProfileManagement
    • enhancement-[branch name] – the enhancement prefix is useful for adding additional functionality to features that already exist such as adding new rooms or new monsters to a game level or adding a search feature to a section of a web site.
      • enhancement-AccountLookupByPhone
    • bugfix-[branch name]-[bug name] – the bugfix prefix is useful for fixing bugs that were found after a branch has been committed, pushed and then merged into the master.
      • bugfix-ProfileManagement-PhoneValidationError
    • The branch that you create will be a clone of the branch that your project is currently on. The feature branch above would contain all of the code of the master branch.
  • Leave “Checkout New Branch” checked
  • Click Create Branch


It is important to always keep track of which branch that you are on so that you don’t make changes to branch that you are not intending to modify. There are ways to correct this but it is always easier to check your branch first. You can easily tell which branch a project is on by looking at the repository menu in SourceTree.

Committing Changes to a Branch

  • Once you have made code changes in a branch SourceTree will show that there are Uncommitted changes.
  • Click on the “Uncommitted changes” label in your main window
  • Click the Unstaged files check box to move all modified and new files to the staged files section.
    • The yellow icon indicates that this file was previously committed to source control but has since been modified.
    • The blue icon indicates that this is a new file and is not currently tracked by source control
  • Click the Commit button on the top menu bar
  • Enter a commit message and click the Commit button

Pushing Your Branch to BitBucket

By pushing your branch to BitBucket you are able to keep all your branches stored online so that all code is backed up to your repository in case of computer failure without having to recreate your branches or separate code features. This also allows you to create new enhancements or features off of an existing branch.

  • Click the Push button on the top menu bar
  • Click the check box next to your feature and uncheck any other boxes checked since we only want changes from our new branched pushed to our repository.
  • Click OK to push your changes.

Merging Branches to Master

Branches are a way to make additions and modifications to our code in a safe manner so that if there are issues we can switch back to another working branch. However once a branch has been create and tested it eventually needs to be merged into the master branch to be included as part of the final project. In order to do this safely we need to do two merges.

  • In SourceTree with our new branch as the active branch (the active branch is bold) right click on the master branch and select “Merge master into current branch”
    • We merge master into the new branch and then test our project to make sure that there are no unexpected issues when the branches are merged
  • Click OK to confirm the merge
  • Test your code in your current branch
    • If everything is working as expected continue
    • If there are unexpected issues, correct as needed and then merge master again into the current branch and test again. Repeat as needed.
  • Double click on the master branch to make it the active branch (it should be bold)
  • Right click on your new branch and select “Merge [branch name] into current branch
  • Click OK to confirm the merge
  • Again test your application to make sure the merge was successful and all features are now in your master branch as expected
  • Push your master branch to BitBucket.

Stashing Changes When Made in the Wrong Branch

Many times changes can be made in the wrong branch (making changes in the master branch rather than in a feature or enhancement branch for example). When this happens we can “stash” the changes, switch branches and then “pop” the changes to the correct branch before we commit them.

  • In SourceTree click on the “Uncommitted changes” in your main area.
  • Click on the Stash icon in the top menu
  • Add a message about the stash, click OK
  • Double click on the correct branch that the changes should have been made in so that it is the active branch
  • Expand the “Stashes” menu option in the middle panel and you should see the stash message that you entered.
  • Right click on the stash and select “Apply Stash [stash name]”
  • Click the OK button
  • Check your code to make sure that the changes in the stash were applied to your current branch. If not you can try again or fix any issues that were displayed
  • When your changes have been applied to the correct branch, right click on the stash and select “Delete Stash [stash name]”

Test your code with the stashed changes applied to the correct branch. Make any changes that are needed and then when ready commit and push your changes.

Using Source Control – Part 2 (PDF)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s