从命令行获取 git 提交的 url

发布于 2025-01-13 07:10:36 字数 294 浏览 1 评论 0原文

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

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

相关的

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

I like to share links to git commits with other people. It's useful to be able to get these without too much clicking in emacs, there is a package in emacs that I use (https://github.com/sshaw/git-link) but I want to do this from the command line.

Is there an easy way to get a link to a commit from from the command line? (I use github)

Related

git rev-parse HEAD gives you the commit from the command line

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

瀟灑尐姊 2025-01-20 07:10:36

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

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

The URL you'd need for, say, a Bitbucket-hosted Git repository is different from the URL you'd need for a GitHub-hosted Git repository. Git itself doesn't have such links: each hosting system in use has to invent its own.

Since you want a GitHub-specific link, you can generate one, knowing that it will begin with https://github.com/ or https://raw.githubusercontent.com/. After that comes the name of the repository, e.g., git/git/. If you then want a particular file, the next part is blob/, then either a branch name or a commit hash ID, then the path to the file. The same scheme works to obtain raw file content when suing raw.githubusercontent.com.

绝不放开 2025-01-20 07:10:36

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

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

This linux command should work for GitHub if your remote is called 'origin' and you use https to access your origin

echo "$(git config --get remote.origin.url | sed -e 's/\.git$//g')/commit/$(git rev-parse HEAD)"
雨轻弹 2025-01-20 07:10:36

较新版本的 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 中的链接即可。

More recent versions of Git have a git remote get-url subcommand which is nicer than git config --get, particularly because it resolves insteadOf.

Here is a simple script which uses get-url to get the base URL for the current repo (in easy copy and paste form).

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

This can then be used via something like:

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

Which you could configure as a git alias, with something like:

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

You may be interested in a tool I wrote to make git log contain links: https://gist.github.com/dgl/ef848e75c03c09b0db63a43173ee5662/

Your terminal will need to support embedded hyperlinks, but provided it does you can use this combined with git log -1 and simply copy the link from the commit ID.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文