从命令行获取 git 提交的 url

问题描述 投票:0回答:3

我喜欢与其他人分享 git 提交的链接。无需在 emacs 中进行太多点击即可获得这些内容非常有用,我使用 emacs 中的一个包(https://github.com/sshaw/git-link),但我想通过命令来执行此操作线。

有没有一种简单的方法可以从命令行获取提交的链接? (我用

github

相关

git rev-parse HEAD
为您提供从命令行的提交

git github hyperlink
3个回答
2
投票

例如,Bitbucket 托管的 Git 存储库所需的 URL 与 GitHub 托管的 Git 存储库所需的 URL 不同。 Git 本身没有这样的链接:每个正在使用的托管系统都必须发明自己的。

由于您想要一个 GitHub 特定 链接,因此您可以生成一个链接,并知道它将以

https://github.com/
https://raw.githubusercontent.com/
开头。之后是存储库的名称,例如
git/git/
。如果您想要一个特定的文件,下一部分是
blob/
,然后是分支名称或提交哈希 ID,然后是文件的路径。同样的方案可以在起诉时获取原始文件内容
raw.githubusercontent.com


1
投票

如果您的遥控器称为“origin”并且您使用 https 访问您的 origin,则此 linux 命令应该适用于 GitHub

echo "$(git config --get remote.origin.url | sed -e 's/\.git$//g')/commit/$(git rev-parse HEAD)"

0
投票

较新版本的 Git 有一个

git remote get-url
子命令,它比
git config --get
更好,特别是因为它可以解析
insteadOf

这是一个简单的脚本,它使用

get-url
获取当前存储库的基本 URL(以简单的复制和粘贴形式)。

cat <<'EOF' > ~/bin/git-link
#!/usr/bin/perl
use v5.10;

my $remote = (qx(git status -sb) =~ /\.\.\.([a-zA-Z0-9-]+)/)[0] || "origin";
my $remote_uri = qx{git remote get-url "$remote"};

chomp $remote_uri;
# Logic carefully arranged to work for github, sourcehut (sr.ht) and gitlab.
$remote_uri =~ s/^git\@//;
$remote_uri =~ s/\.git$//;
$remote_uri =~ s!^([.\w]+):!https://$1/! unless $remote_uri =~ /^http/;

say $remote_uri;
EOF
chmod +x ~/bin/git-link

然后可以通过以下方式使用:

echo $(git link)/commit/$(git rev-parse HEAD)

您可以将其配置为 git 别名,例如:

$ git config --global alias.clink '!echo $(git link)/commit/$(git rev-parse HEAD)'
$ git clink
https://some-repo/commit/abcde

您可能对我编写的用于使

git log
包含链接的工具感兴趣:https://gist.github.com/dgl/ef848e75c03c09b0db63a43173ee5662/

您的终端需要支持嵌入式超链接,但如果支持的话,您可以将其与

git log -1
结合使用,只需从提交 ID 中复制链接即可。

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