How do you open SourceTree from the command line?

GitCommand LineAtlassian Sourcetree

Git Problem Overview


Is there a quick and easy way to open a git repository in SourceTree from the command line?

I do a lot of git work from Terminal, but sometimes there's no replacement for a good history view/diff. Would love to be able to open without using bookmarks.

Git Solutions


Solution 1 - Git

Installing the SourceTree Command Line Tools will provide you with the stree command. This will allow you to open the current directory in SourceTree.

sourcetree commandline tools

You can also specify a particular path to a repo

stree ~/my-repo-in-another-folder

If installing command-line tools isn't an option for whatever reason, you can also do the following:

open -a SourceTree path-to-file

and maybe set up an alias in .bashrc or .zshrc

alias sourcetree='open -a SourceTree'

For those who are using SourceTree 3

alias sourcetree='open -a SourceTree\ 3'

Solution 2 - Git

The answer by loeschg may not work; some people get an error referring to their system logs and cannot install the command line tools. There is an open issue about this.

A workaround is found here. Use:

ln -s /Applications/SourceTree.app/Contents/Resources/stree /usr/local/bin/

This will create a symbolic link to the stree binary and put it in /usr/local/bin. Make sure that directory is on your path: which stree should result in /usr/local/bin/stree. If it does not, then add it to your PATH manually or use echo export PATH='/usr/local/bin:$PATH' >> ~/.bash_profile, which does it for you (restart your shell to reload the PATH variable).

On the above-mentioned issue's page, another workaround that I didn't test was posted: alias stree='/Applications/SourceTree.app/Contents/Resources/stree'. If you use it, please report in the comments if and how it works and why you'd prefer it over the symbolic link.

For both methods, the path to stree in SourceTree.app must of course match the location where you installed SourceTree.app.

Now, stree is installed and can be accessed from any directory. The shortest way to open SourceTree when your shell's working directory is a repository's root directory is stree ..

Solution 3 - Git

For those of you on Windows, you can add a batch file named stree.bat to a folder in your PATH Environment Variable. (I have a C:\batch folder which is in my PATH where I store all my utility batch files.) Put the following in to your batch file:

@echo off
start "" "C:\Program Files (x86)\Atlassian\SourceTree\SourceTree.exe"

Now you can go to any Git or Mercurial repository and run this command which will open the repository in SourceTree.

Solution 4 - Git

Another Windows solution for those who use Git on the Bash command line (msys).

Add two functions to your Bash .profile:

# Courtesy: http://stackoverflow.com/questions/12015348/msys-path-conversion-or-cygpath-for-msys
function towinpath {
    { cd $1 && pwd -W; } | sed 's|/|\\|g'
}

function stree {
    if [ -z $1 ]; then
        stree_path=$(towinpath pwd)
    else
        stree_path=$(towinpath $1)
    fi

    echo "Starting SourceTree in $stree_path"

    /c/Program\ Files\ \(x86\)/Atlassian/SourceTree/SourceTree.exe -f $stree_path status
}

Reload your shell.

Now you can use:

$ towinpath /c/Temp

And it will echo c:\Temp.

Or you can open SourceTree:

$ stree .

And it will open this repository in SourceTree defaulting to the Status panel.

Solution 5 - Git

If you have cygwin installed, you can use this as your stree.bat. This batch file uses cygpath to resolve . to its absolute path, so you can do stree .

@echo off
FOR /F "tokens=* USEBACKQ" %%F IN (`cygpath -w -a %1`) DO (
SET STREE_OPEN_PATH=%%F
)
%USERPROFILE%\AppData\Local\SourceTree\SourceTree.exe -f "%STREE_OPEN_PATH%"

Solution 6 - Git

in Windows using powershell, from inside directory that you want to open in SourceTree:

& 'C:\Users\userexample\AppData\Local\SourceTree\SourceTree.exe' -f (Get-Location)

N.B: the path C:\Users\userexample\AppData\Local\SourceTree\SourceTree.exe can be changed to whatever SourceTree is installed,

for Exp: if SourceTree is installed with admin privileges this path will be C:\Program Files (x86)\Atlassian\SourceTree\SourceTree.exe and the command will become

& 'C:\Program Files (x86)\Atlassian\SourceTree\SourceTree.exe' -f (Get-Location)

Solution 7 - Git

Windows

Adapting from multiple answers here for Windows, these scripts will allow you to get SourceTree running from command line (tested on SourceTree 3.0.1.7 / Windows 10).

Scripts in a PATH directory

I've placed both these scripts in a folder that is in my system PATH. You won't have to modify your bash profile for this script.

Git Bash for Windows

Create a file named stree (touch stree) in your PATH linked directory and run chmod u+x stree on this file.

#!/bin/sh

function towinpath {
    { cd $1 && pwd -W; } | sed 's|/|\\|g'
}

if [ -z $1 ]; then
    stree_path=$(towinpath pwd)
else
    stree_path=$(towinpath $1)
fi

$LOCALAPPDATA/SourceTree/SourceTree.exe -f $stree_path log &

You can replace "log" in the last line with "status" if you prefer the changes/working directory view of your repository in SourceTree.

Command Prompt or Powershell

Create a file named stree.cmd in your PATH linked directory.

@echo off
start "" "%LOCALAPPDATA%\SourceTree\SourceTree.exe"

Note that this won't actually open up the directory as a repository.

Please feel free to improve the scripts, especially the one for Command Prompt.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionloeschgView Question on Stackoverflow
Solution 1 - GitloeschgView Answer on Stackoverflow
Solution 2 - GitErikView Answer on Stackoverflow
Solution 3 - GitSumner EvansView Answer on Stackoverflow
Solution 4 - GitGreg BurghardtView Answer on Stackoverflow
Solution 5 - GitYan SernView Answer on Stackoverflow
Solution 6 - GitSouhaiebView Answer on Stackoverflow
Solution 7 - GitDheeraj ChakilamView Answer on Stackoverflow