SourceTree and Git: How to use it

Summary

This tutorial will talk about how to use SourceTree and git for newcomers.
I will explain how to create, manage and evolve repositories in SourceTree workflow.

Installation

Donwload SourceTree on http://www.sourcetreeapp.com/

Screenshot of the official website

Website of SourceTree application

Install it, there is nothing important to do here.

Setup

Launch SourceTree

Enter your name and your email address (it will be useful for identifying who has made modifications), and ckecked all the boxes, then click on next button.

SourceTree - Setup 1

Enter your login and password for services you want to use, then click on finish.
A test connection is launched, maybe you have to re-enter your password, and succeed if it’s good.

SourceTree - Setup 2

You can add local repositories already created in your computer.
You specify the folder to scan and click on Search Now.
You can choose which repositories you want to import in SourceTree

SourceTree - Setup 3

You can change the destination folder for cloning a repository in:
SourceTree -> Settings -> General -> Misc -> Project Folder

Bookmarks

The first screen you will have when you launch SourceTree is bookmarks.
It’s a list of all your repositories that you added.

SourceTree - Bookmarks

You see the title, the path, the current branch (git), and if you are up to date:

  • sourcetree-icon-clean: nothing to commit, no missing or modified files.
  • sourcetree-icon-missing: 55 missing files
  • sourcetree-icon-modified: 2 files modified
  • sourcetree-icon-not-tracked: 1 file not add in the index (tracked)
  • sourcetree-icon-pull: you have to pull. There is 8 commits on the server’s repository further than your repository
  • sourcetree-icon-push: you have to push. You have 1 version further than the server’s repository

Create a git repository

If you want to start a new repository you click on Hosted Projects sourcetree-icon-hosted-projects in Bookmarks window.

Here we have the list of all repositories you have access.
You have project name, description, service’s host, owner and the lock if it’s private repository.

sourcetree-hosted-repositories

Click on the button Create New Repository.
In this window you can choose the service, the owner, if it’s publicly visible the type (select git).
Just add a name and if you want a description.

sourcetree-add-repository

Now you can clone repository by double clicking on it.
Maybe a popup will demand credential about using your ssh key in your keyring, always accepted it.
In the new window you can choose a destination folder and a bookmark name.
The default project folder can be set in settings’ SourceTree.
When it’s ok, click on clone button.

sourcetree-clone-repository

Import a git repository

Click on Hosted Projects sourcetree-icon-hosted-projects in Bookmarks window.

Here we have the list of all repositories you have access.

sourcetree-hosted-repositories

Double clicking on the repository you want to clone.
Maybe a popup will demand credential about using your ssh key in your keyring, always accepted it.
In the new window you can choose a destination folder and a bookmark name.
When it’s ok, click on clone button.

sourcetree-clone-repository

Overview

This window show files and commit

sourcetree-repository-overview

Take a look on the toolbar

sourcetree-toolbar

  • Commit: validate files stored in the index and save it with a comment
  • Checkout: retrieve files in a particular commit/tag/branch
  • Reset: restore files until selected commit (erase and modified your local files)
  • Stash: when you to change branch but you can’t commit (unfinished task), you can put it in a Work In Progress workflow
  • Add: add files in the index
  • Remove: remove files in the index
  • Add/Remove: add/remove files in the index
  • Fetch: retrieve all commits from all branchs from server’s repository
  • Pull: retrieve distant commits from server’s repository
  • Push: take all local commits and send it to server’s repository
  • Branch: create a branch
  • Merge: merge a branch
  • Tag: add tag, useful if you want to put a checkpoint like v1.0 in the branch

To see your folder/files, right click in the file’s list and choose Show In Finder.

First commit and push

Add

Create a file in your folder.
Automatically the panel Files in the working tree will be update.

sourcetree-new-file

You can drag and drop selected file to the panel Files staged in the index.
You can also select the file and click on Add in the toolbar.

sourcetree-new-file-added

Commit

Now you can commit by clicking on the button in toolbar.
A new window appear, you set a message in the commit message

sourcetree-commit-message

Push

Clicking in toolbar on Push button.
In this window you can choose on wich remote branch you want to push, just check Push? (in first push you can’t select a branch)

sourcetree-push