我想访问我的存储库网页:
Github.com/myUsername/myRepoName 在终端中导航到
Github.com/myUsername/myRepoName 在终端中导航到
myRepoName
目录后,我能否运行一个命令在浏览器中打开该链接?Git没有内置此功能,但很容易实现为别名或基于脚本的命令。
您的操作系统和使用的Git发行版以及在存储库中使用的URL类型将影响您问题的确切答案,但是我下面的两个解决方案中至少有一个应该很容易适应大多数系统。
这是一个别名,在我的Git for Windows ~/.gitconfig
中适用于https URL,没有错误检查:
[alias]
browse-remote = !/c/Program\\ Files\\ \\(x86\\)/Google/Chrome/Application/chrome.exe $(git remote show origin | grep Fetch.URL | sed 's/.*http/http/')
git browse-remote
来在Chrome中启动远程。如果您想使用这个想法,您需要正确设置浏览器路径,并确保提取URL的部分在您的计算机上正常工作。git-browse-remote
:#!/bin/bash
# Set this to your browser
BROWSER='/c/Program Files (x86)/Google/Chrome/Application/chrome.exe'
set -o pipefail
if URL=$(git remote show origin | grep Fetch.URL | sed 's/.*http/http/'); then
"$BROWSER" "$URL"
else
echo git-browse-remote: Could not extract repo URL from the output of \"git remote show origin\"
exit 1
fi
chmod +x git-browse-remote
并将文件放在您的PATH某个位置。git browse-remote
,如果它是一个http(s)链接,则会在浏览器中打开远程连接;如果您不在repo内或者远程URL不是http(s)链接,则会显示错误消息。git@github.com:user/repo.git
,我建议采用第二种解决方案,并向脚本添加一些URL转换逻辑。我写了一个命令行界面(to-where-cli),可以帮助您解决这个问题,只需执行:
npm install -g to-where-cli
// open current remote branch
tw git open
// open master/main remote branch
tw git open -m