有没有一种从命令行在SourceTree中打开git存储库的快捷方法?
我从终端做了很多git工作,但有时候没有替代好的历史视图/差异。希望能够在不使用书签的情况下打开。
安装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'
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'
。如果您使用它,请在评论中报告它是否以及如何工作以及为什么您更喜欢它而不是符号链接。
对于这两种方法,stree
中SourceTree.app
的路径当然必须与安装SourceTree.app
的位置相匹配。
现在,stree
已安装,可以从任何目录访问。当shell的工作目录是存储库的根目录时,打开SourceTree的最短路径是stree .
。
对于Windows上的用户,可以将名为stree.bat
的批处理文件添加到PATH环境变量中的文件夹中。 (我有一个C:\batch
文件夹,它位于我的PATH中,我存储了所有的实用程序批处理文件。)将以下内容放入批处理文件中:
@echo off
start "" "C:\Program Files (x86)\Atlassian\SourceTree\SourceTree.exe"
现在,您可以转到任何Git或Mercurial存储库并运行此命令,该命令将在SourceTree中打开存储库。
另一种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中打开此存储库默认为“状态”面板。
如果您安装了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%"
这些脚本适用于Windows的多个答案,允许您从命令行运行SourceTree(在SourceTree 3.0.1.7 / Windows 10上测试)。
我已将这两个脚本放在系统PATH中的文件夹中。您不必修改此脚本的bash配置文件。
在PATH链接目录中创建名为stree
(touch 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”。
在PATH链接目录中创建名为stree.cmd
的文件。
@echo off
start "" "%LOCALAPPDATA%\SourceTree\SourceTree.exe"
请注意,这实际上不会将目录打开为存储库。
请随意改进脚本,尤其是命令提示符。