loeschg
loeschg

Reputation: 30581

How do you open SourceTree from the command line?

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.

Upvotes: 108

Views: 50946

Answers (7)

Sumner Evans
Sumner Evans

Reputation: 9157

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 "" "%LocalAppData%\SourceTree\SourceTree.exe" -f "%cd%"

(credit @Collin K below for the updated command)

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

Old answer:

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

Upvotes: 16

Souhaieb
Souhaieb

Reputation: 639

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)

Upvotes: 2

Dheeraj Chakilam
Dheeraj Chakilam

Reputation: 96

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.

Upvotes: 1

loeschg
loeschg

Reputation: 30581

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'

Upvotes: 151

Yan Sern
Yan Sern

Reputation: 61

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%"

Upvotes: 3

Erik
Erik

Reputation: 4563

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

Upvotes: 47

Greg Burghardt
Greg Burghardt

Reputation: 18783

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.

Upvotes: 6

Related Questions