我可以配置git以便从GitHub克隆吗?

时间:2019-06-30 22:09:36

标签: git github

我希望能够使用git clone user/repo之类的东西从GitHub克隆存储库,而不用输入整个URL。可以在git中配置它,还是应该为此单独执行一个命令?

4 个答案:

答案 0 :(得分:4)

也许您正在寻找的是hub

  

用法

     
$ hub clone rtomayko/tilt

# expands to:
$ git clone git://github.com/rtomayko/tilt.git
     

可以将集线器安全地别名为git,以便您可以在shell中键入$ git <command>并获得所有常用的hub功能。

答案 1 :(得分:4)

如果您不介意再输入几个字符,

git config --global url."https://github.com/".insteadOf g://
git clone g://user/repo

g://只是伪造的协议,将由https://github.com/代替。 g://在这里代表githubg部分可以命名为其他单词。

例如,对于特定用户tom,您甚至可以使用

git config --global url."https://github.com/tom/".insteadOf t://
git clone t://repo

t://repo将替换为https://github.com/tom/repo

答案 2 :(得分:2)

您可以这样设置git别名:

clone-github = "!f(){ repo=$1; shift; git clone \"https://github.com/$repo\" \"$@\"; }; f"

然后像git clone-github user/repo一样使用它,例如:

$ git clone-github octocat/Spoon-Knife
Cloning into 'Spoon-Knife'...
remote: Enumerating objects: 16, done.
remote: Total 16 (delta 0), reused 0 (delta 0), pack-reused 16
Unpacking objects: 100% (16/16), done.

答案 3 :(得分:1)

您可以使用SSH的快捷方式。将以下内容放入~/.ssh/config

Host gh
  Hostname github.com
  User git
  Protocol 2

现在您可以使用以下内容:

git clone gh:user/repo

# equivalent to:
git clone git@github.com:user/repo

此解决方案的妙处在于您也可以将其用于其他提供程序:

Host gl
  Hostname gitlab.mycompany.com
  User git
  Protocol 2

然后:

git clone gl:user/repo

# equivalent to:
git clone git@gitlab.mycompany.com:user/repo