如何从命令行打开 SourceTree?

是否有一种快速简单的方法从命令行在 SourceTree 中打开 git 存储库?

我做了很多 git 工作从终端,但有时没有替代良好的历史查看/差异。希望能够不使用书签打开。

42498 次浏览

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'

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.

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.

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

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

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.

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)