如何从命令行打开SourceTree?

问题描述 投票:60回答:6

有没有一种从命令行在SourceTree中打开git存储库的快捷方法?

我从终端做了很多git工作,但有时候没有替代好的历史视图/差异。希望能够在不使用书签的情况下打开。

git command-line atlassian-sourcetree
6个回答
93
投票

安装SourceTree命令行工具将为您提供stree命令。这将允许您在SourceTree中打开当前目录。

您还可以指定仓库的特定路径

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

如果出于任何原因无法安装命令行工具,您还可以执行以下操作:

open -a SourceTree path-to-file

并且可能在.bashrc或.zshrc中设置别名

alias sourcetree='open -a SourceTree'

对于那些使用SourceTree 3的人

alias sourcetree='open -a SourceTree\ 3'

34
投票

The answer by loeschg可能不起作用;有些人在引用系统日志时出错,无法安装命令行工具。 There is an open issue about this.

A workaround is found here.使用:

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

这将创建一个指向stree二进制文件的符号链接并将其放入/usr/local/bin。确保该目录在您的路径上:which stree应该导致/usr/local/bin/stree。如果没有,那么手动将它添加到你的PATH或使用echo export PATH='/usr/local/bin:$PATH' >> ~/.bash_profile,它为你做(重新启动你的shell重新加载PATH变量)。

在上述问题的页面上,我发布了另一个没有测试的解决方法:alias stree='/Applications/SourceTree.app/Contents/Resources/stree'。如果您使用它,请在评论中报告它是否以及如何工作以及为什么您更喜欢它而不是符号链接。

对于这两种方法,streeSourceTree.app的路径当然必须与安装SourceTree.app的位置相匹配。

现在,stree已安装,可以从任何目录访问。当shell的工作目录是存储库的根目录时,打开SourceTree的最短路径是stree .


9
投票

对于Windows上的用户,可以将名为stree.bat的批处理文件添加到PATH环境变量中的文件夹中。 (我有一个C:\batch文件夹,它位于我的PATH中,我存储了所有的实用程序批处理文件。)将以下内容放入批处理文件中:

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

现在,您可以转到任何Git或Mercurial存储库并运行此命令,该命令将在SourceTree中打开存储库。


5
投票

另一种Windows解决方案,适用于在Bash命令行(msys)上使用Git的用户。

向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
}

重新加载你的shell。

现在你可以使用:

$ towinpath /c/Temp

它将回应c:\Temp

或者你可以打开SourceTree:

$ stree .

它将在SourceTree中打开此存储库默认为“状态”面板。


1
投票

如果您安装了cygwin,可以将其用作stree.bat。此批处理文件使用cygpath.解析为其绝对路径,因此您可以执行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%"

0
投票

Windows

这些脚本适用于Windows的多个答案,允许您从命令行运行SourceTree(在SourceTree 3.0.1.7 / Windows 10上测试)。

PATH目录中的脚本

我已将这两个脚本放在系统PATH中的文件夹中。您不必修改此脚本的bash配置文件。

Git Bash for Windows

在PATH链接目录中创建名为streetouch stree)的文件,并在此文件上运行chmod u+x stree

#!/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 &

如果您更喜欢SourceTree中存储库的更改/工作目录视图,则可以将最后一行中的“log”替换为“status”。

Command Prompt or Powershell

在PATH链接目录中创建名为stree.cmd的文件。

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

请注意,这实际上不会将目录打开为存储库。

请随意改进脚本,尤其是命令提示符。

© www.soinside.com 2019 - 2024. All rights reserved.