100

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.

loeschg
  • 29,961
  • 26
  • 97
  • 150

7 Answers7

139

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'
DanH
  • 492
  • 1
  • 7
  • 14
loeschg
  • 29,961
  • 26
  • 97
  • 150
  • 11
    You can make your sourcetree alias open the git repository that's in the current working directory by changing it to `alias sourcetree='open -a SourceTree ./'`. Also Mac users may wish to place the alias in `.bash_profile` – Leigh McCulloch Jul 15 '14 at 00:58
47

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

Community
  • 1
  • 1
Erik
  • 4,305
  • 3
  • 36
  • 54
  • I prefer the `alias` method because it feels less "permanent" and more "portable" than creating a symbolic link in a directory that's already relatively full and feels like it's being managed automatically for me – Homebrew likes to symlink stuff from its Cellar there. Plus, SourceTree has been making changes recently to its UI that I don't appreciate, so I've started keeping multiple versions of it around, just in case. Using alias(es) gives me greater flexibility as to which version I'd like to target in bash. – Illya Moskvin Apr 07 '18 at 22:21
  • 2
    I was also getting this error. What worked for me: simply update SourceTree to v3.0.1 (Sourcetree --> Check for Updates...). After updating it, Installing Command Line Tools was then working fine. :D (my Sourcetree was still on v2.7.6 when I got the error) – Buju May 15 '19 at 14:28
12

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.

Sumner Evans
  • 8,951
  • 5
  • 30
  • 47
  • 3
    FYI: On my machine source tree is NOT installed in Program Files, its in C:\Users\\AppData\Local\SourceTree. Probably because they're using the Squirrel installer now? – Kevin Berridge Jan 11 '18 at 16:07
  • The command that works for me in 2023 is `start "" "%LocalAppData%\SourceTree\SourceTree.exe" -f "%cd%"` – Collin K Jul 11 '23 at 20:36
6

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.

Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92
  • Awesome, thanks! There's also a Cygwin function, `cygpath`, where you could do something akin to `function towinpath { cygpath -w $(cd $1 && pwd); }`. This will more robustly handle things such as UNC paths. – eacousineau Nov 23 '16 at 20:35
3

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%"
ascripter
  • 5,665
  • 12
  • 45
  • 68
Yan Sern
  • 61
  • 2
2

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 wherever 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)
Souhaieb
  • 559
  • 8
  • 12
1

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.

Community
  • 1
  • 1