发布于 2026-01-06 0 阅读
0

向 Git 添加远程快捷方式

向 Git 添加远程快捷方式

原文发布于https://hoelz.ro/blog/adding-remote-shortcuts-to-git

如果你和我一样,很可能你是一位 Git 用户,并且经常使用少数几个主机来托管代码仓库。我在这里举的例子是GitHub

要克隆其他用户的仓库,你最终会输入类似这样的命令:

  $ git clone https://github.com/miyagawa/cpanminus.git
Enter fullscreen mode Exit fullscreen mode

如果你想克隆你的某个仓库,最终会得到类似这样的结果:

  $ git clone git@github.com:hoelzro/linotify.git
Enter fullscreen mode Exit fullscreen mode

虽然打字量​​不算,但肯定有更快捷的方法!要是能直接打字就好了,对吧?

  $ git clone github:miyagawa/cpanminus
Enter fullscreen mode Exit fullscreen mode

或者这样?

  $ git clone hoelzro:linotify
Enter fullscreen mode Exit fullscreen mode

其实,只需对 .gitconfig 文件进行一些修改,就可以做到!

你可以在 gitconfig 文件中添加一个 URL 部分,并使用 insteadOf 属性来描述你想要使用的前缀。以下是我的 .gitconfig 文件中前两个示例的实现方式:

[url "git@github.com:hoelzro/"]
    insteadOf = hoelzro:
[url "https://github.com/"]
    insteadOf = github:
Enter fullscreen mode Exit fullscreen mode

对不同的水源重复上述步骤!

文章来源:https://dev.to/hoelzro/adding-remote-shortcuts-to-git-38he