计算机编程
2023年4月25日星期二
如何使用命令行克隆私有的GitHub仓库
取代密码验证的新方式
克隆远程仓库上的文件

在GitHub克隆仓库的操作应该每个人都很熟悉。但是克隆一个私有仓库该怎么做呢?答案是使用同样的命令。

git clone https://github.com/yira97/Yiran.git
clone-public-repo.sh

随后,git会提示你输入用户名和密码。但是如果你直接输入你GitHub的用户名的账号和密码的话会发现并不能通过验证。同时你得到了下面的错误信息。

git clone https://github.com/yira97/PineHamster
# Cloning into 'PineHamster'...
# Username for 'https://github.com': yira97
# Password for 'https://yira97@github.com': 
# remote: Support for password authentication was removed # on August 13, 2021.
# remote: Please see https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.
# fatal: Authentication failed for 'https://github.com/yira97/PineHamster/'
error.sh

从错误提示中可以得知,自2021年8月21日开始,GitHub就已不再支持密码验证了,现在推荐的方式是 “personal access token”。

https://github.com/settings/tokens

来到 ”Developer settings” 这里,可以创建新的token令牌。创建好后,保存好最后得到的令牌,重新执行一次 "git clone path/to/your/repo"。

git clone https://github.com/yira97/PineHamster.git
# Cloning into 'PineHamster'...
# Username for 'https://github.com': yira97
# Password for 'https://yira97@github.com': 
# remote: Enumerating objects: 1583, done.
# remote: Counting objects: 100% (1583/1583), done.
# remote: Compressing objects: 100% (851/851), done.
# remote: Total 1583 (delta 690), reused 1382 (delta 489), # pack-reused 0
# Receiving objects: 100% (1583/1583), 24.64 MiB | 21.04 MiB/s, done.
# Resolving deltas: 100% (690/690), done.
clone-private-repo.sh

这一次,在提示输入密码的时候,粘贴进刚才保存的令牌。随后你就会发现克隆顺利的完成了。