Sourcetree

Version ControlGUIGitMercurialAtlassianClientVisualDesktop

Git and Mercurial GUI Client

Sourcetree

Overview

Sourcetree is a free Git and Mercurial desktop client developed by Atlassian that provides a visual interface for version control operations. Designed to make Git and Mercurial accessible to developers who prefer graphical interfaces over command-line tools, Sourcetree offers intuitive visualizations of repository history, branching, and merging operations. Available for Windows and macOS, Sourcetree integrates seamlessly with Atlassian's ecosystem while supporting any Git or Mercurial repository.

Details

Sourcetree bridges the gap between command-line version control and user-friendly visual interfaces. The application provides interactive graphs showing repository history, making it easy to understand complex branching structures and merge relationships. Users can perform most Git and Mercurial operations through point-and-click interactions while still having access to terminal commands when needed.

Key features include visual commit history with interactive graphs, drag-and-drop branching and merging, built-in diff and merge tools, and integration with popular Git hosting services like GitHub, GitLab, and Bitbucket. Sourcetree also supports Git Flow and Git LFS (Large File Storage) workflows.

The application excels at making complex Git operations accessible to developers who are new to version control or those who prefer visual workflows. Features like interactive rebase, cherry-picking, and conflict resolution are presented in intuitive interfaces that reduce the learning curve typically associated with these operations.

Sourcetree includes built-in terminal access, allowing users to execute command-line operations when GUI options aren't sufficient, providing the best of both worlds for different user preferences and workflow requirements.

Advantages and Disadvantages

Advantages

  • Visual Repository History: Clear graphical representation of commits and branches
  • User-Friendly Interface: Intuitive GUI reduces learning curve
  • Free License: No cost for individual or commercial use
  • Multi-Platform: Available on Windows and macOS
  • Git Flow Integration: Built-in support for Git Flow workflows
  • Conflict Resolution: Visual merge conflict resolution tools
  • Repository Management: Easy management of multiple repositories
  • Atlassian Integration: Seamless connection to Bitbucket and Jira

Disadvantages

  • Performance Issues: Can be slow with large repositories
  • Limited Customization: Fewer customization options than command-line
  • Platform Limitations: Not available on Linux
  • Resource Usage: Higher memory consumption than command-line tools
  • Atlassian Account Required: Requires free Atlassian account for usage
  • Advanced Operations: Some advanced Git features require command-line
  • Update Frequency: Updates can be slower than other Git clients

Reference Pages

Code Examples

Initial Setup and Repository Management

# Download and install Sourcetree from official website
# During setup, configure Git and Mercurial paths

# Adding Git configuration (can be done via Sourcetree preferences)
git config --global user.name "Your Name"
git config --global user.email "[email protected]"

# Git configuration for Sourcetree (in Tools > Options > Git)
# Default push behavior: Simple
# Use system Git: Recommended for latest features

Repository Operations Through Sourcetree GUI

Cloning Repositories

1. Click "Clone" button in Sourcetree
2. Enter repository URL:
   - GitHub: https://github.com/user/repository.git
   - GitLab: https://gitlab.com/user/repository.git
   - Bitbucket: https://bitbucket.org/user/repository.git
3. Choose local destination folder
4. Click "Clone" to download repository

Basic File Operations

Working with Files:
1. Stage files: Drag from "Unstaged files" to "Staged files"
2. Unstage files: Drag from "Staged files" to "Unstaged files"
3. Discard changes: Right-click file > "Discard changes"
4. View differences: Click file to see diff in bottom panel

Committing Changes:
1. Stage desired files
2. Enter commit message in text box
3. Click "Commit" button
4. For push: Click "Push" button in toolbar

Branching Operations

Creating and Managing Branches

Create Branch:
1. Right-click on commit in history
2. Select "Branch from this commit"
3. Enter branch name
4. Choose to checkout immediately (recommended)

Switch Branches:
1. Double-click branch name in sidebar
2. Or: Right-click branch > "Checkout"

Merge Branches:
1. Checkout target branch (usually main/master)
2. Right-click source branch
3. Select "Merge [branch-name] into current branch"
4. Resolve conflicts if any appear

Git Flow Workflow

Initialize Git Flow:
1. Go to Repository > Git Flow > Initialize Repository
2. Configure branch naming conventions:
   - Master branch: main (or master)
   - Develop branch: develop
   - Feature prefix: feature/
   - Release prefix: release/
   - Hotfix prefix: hotfix/

Start Feature:
1. Repository > Git Flow > Start New Feature
2. Enter feature name
3. Sourcetree creates feature branch and switches to it

Finish Feature:
1. Repository > Git Flow > Finish Current Feature
2. Sourcetree merges feature back to develop
3. Deletes feature branch
4. Switches back to develop

Conflict Resolution

Merge Conflict Handling

When conflicts occur:
1. Sourcetree shows conflicted files with warning icon
2. Click conflicted file to open merge tool
3. Use built-in merge tool or external tool:
   - Left panel: Your changes
   - Center panel: Result
   - Right panel: Their changes
4. Choose changes to keep or manually edit
5. Click "Save" when resolved
6. Stage resolved files
7. Complete merge commit

External Merge Tools Configuration

Configure External Tools (Tools > Options > Diff):
- External Diff Tool: Beyond Compare, KDiff3, WinMerge
- Merge Tool: P4Merge, TortoiseMerge, VS Code

Command-line equivalents for reference:
git config --global merge.tool p4merge
git config --global mergetool.p4merge.path "C:/Program Files/Perforce/p4merge.exe"

Advanced Features

Interactive Rebase

Interactive Rebase in Sourcetree:
1. Right-click on base commit
2. Select "Rebase children of [commit] interactively"
3. Reorder, squash, or edit commits in dialog
4. Sourcetree guides through each step

Command-line equivalent:
git rebase -i HEAD~3

Cherry Picking

Cherry Pick Commits:
1. Right-click commit from any branch
2. Select "Cherry Pick"
3. Choose target branch if not current
4. Resolve conflicts if any
5. Complete cherry pick

Command-line equivalent:
git cherry-pick commit-hash

Stashing Changes

Stash Workflow:
1. Click "Stash" button in toolbar
2. Enter stash description
3. Choose to include untracked files if needed
4. View stashes in left sidebar
5. Apply stash: Right-click > "Apply Stash"
6. Delete stash: Right-click > "Delete Stash"

Command-line equivalents:
git stash push -m "Description"
git stash list
git stash apply stash@{0}
git stash drop stash@{0}

Repository History and Analysis

Viewing History

History Navigation:
1. Use graph view to see branch relationships
2. Click commits to see details in bottom panel
3. Use search box to find specific commits
4. Filter by author, date range, or file path

File History:
1. Right-click file > "Log Selected"
2. View all changes to specific file
3. Compare versions by selecting two commits

Command-line equivalent:
git log --graph --oneline --all
git log --follow filename.txt

Blame/Annotation

File Annotation:
1. Right-click file > "Blame"
2. View line-by-line authorship
3. Click line to see commit details
4. Navigate through file history

Command-line equivalent:
git blame filename.txt

Remote Repository Management

Managing Remotes

Add Remote:
1. Repository > Repository Settings
2. Click "Add" under Remotes
3. Enter remote name and URL
4. Click OK

Fetch/Pull/Push:
1. Fetch: Updates remote tracking branches
2. Pull: Fetch + merge into current branch
3. Push: Send local commits to remote

Command-line equivalents:
git remote add origin https://github.com/user/repo.git
git fetch origin
git pull origin main
git push origin main

Pull Requests (for GitHub/GitLab/Bitbucket)

Create Pull Request:
1. Push feature branch to remote
2. In Sourcetree: Repository > Create Pull Request
3. Browser opens to hosting service
4. Fill in PR details and submit

Note: Actual PR creation happens on hosting platform

Integration with Development Workflow

Pre-commit Hooks

# Example pre-commit hook (place in .git/hooks/pre-commit)
#!/bin/sh
# Run tests before commit
npm test
if [ $? -ne 0 ]; then
    echo "Tests failed. Commit aborted."
    exit 1
fi

# Make executable
chmod +x .git/hooks/pre-commit

Sourcetree with CI/CD

Workflow Integration:
1. Use Sourcetree for local development
2. Push branches trigger CI pipelines
3. Monitor build status in hosting platform
4. Use Sourcetree for merging after CI passes

Branch Protection:
- Configure on hosting platform
- Prevents direct pushes to main/master
- Requires PR reviews and CI checks

Customization and Productivity

Keyboard Shortcuts (Windows)

Common Shortcuts:
- Ctrl+N: New repository/clone
- Ctrl+O: Open repository
- Ctrl+R: Refresh
- Ctrl+Shift+C: Clone
- F5: Refresh current view
- Ctrl+1: File Status view
- Ctrl+2: History view
- Ctrl+3: Search view

Custom Actions

Add Custom Actions (Tools > Options > Custom Actions):
1. Menu caption: "Open in VS Code"
2. Script to run: code $REPO
3. Parameters: Repository root
4. Show Full Output: Checked

Example actions:
- Open terminal in repo: cmd /k cd $REPO
- Run tests: npm test (in repo directory)
- Open file manager: explorer $REPO

Troubleshooting Common Issues

Performance Optimization

Large Repository Handling:
1. Tools > Options > Git
2. Enable "Use shallow clones where possible"
3. Reduce history display limit
4. Use .gitignore for unnecessary files

Repository Cleanup:
git gc --aggressive  # Run from terminal in Sourcetree
git prune  # Remove unreachable objects

Authentication Issues

Authentication Setup:
1. Tools > Options > Authentication
2. Add account for hosting service
3. Use Personal Access Tokens for GitHub/GitLab
4. Use App Passwords for Bitbucket

SSH Key Setup:
1. Tools > Create or Import SSH Keys
2. Generate new key pair
3. Add public key to hosting service
4. Test connection in Sourcetree