我正在尝试使用个人访问令牌与GitHub进行身份验证。在GitHub的帮助文件中,它声明使用cURL方法进行身份验证(创建个人访问令牌)。我已经尝试过了,但是我仍然无法推送到GitHub。请注意,我正试图从未经验证的服务器(特拉维斯CI)推送。

cd $HOME
git config --global user.email "emailaddress@yahoo.com"
git config --global user.name "username"

curl -u "username:<MYTOKEN>" https://github.com/username/ol3-1.git
git clone --branch=gh-pages https://github.com/username/ol3-1.git gh-pages

cd gh-pages
mkdir buildtest
cd buildtest
touch asdf.asdf

git add -f .
git commit -m "Travis build $TRAVIS_BUILD_NUMBER pushed to gh-pages"
git push -fq origin gh-pages

这段代码会导致以下错误:

remote:匿名访问scuzzlebuzz /ol3-1。git否认。

致命:'https://github.com/scuzzlebuzzle/ol3-1.git/'认证失败"


当前回答

如果你正在使用GitHub企业,克隆存储库或推送给你一个403错误,而不是提示输入用户名/令牌,你可以使用这个:

删除存储库 打开命令提示符并导航到您希望存储库所在的文件夹 类型: git克隆https://[USERNAME]:[TOKEN]@[GIT_ENTERPRISE_DOMAIN]/[ORGANIZATION]/[REPO].git

其他回答

自动化/使用OAuth令牌的Git自动化

$ git clone https://github.com/username/repo.git
  Username: your_token
  Password:

它也适用于git push命令。

参考: https://help.github.com/articles/git-automation-with-oauth-tokens/

首先,需要创建一个个人访问令牌(PAT)。描述在这里:https://help.github.com/articles/creating-an-access-token-for-command-line-use/

可笑的是,这篇文章告诉你如何创建它,但完全没有提供任何关于如何使用它的线索。在搜索了大约一个小时的文档和Stack Overflow后,我终于找到了答案:

$ git clone https://github.com/user-or-organisation/myrepo.git
Username: <my-username>
Password: <my-personal-access-token>

实际上,当我在远程工作时,公司的政策迫使我启用双因素身份验证,但仍然有本地更改,所以实际上我需要的不是克隆,而是推送。我在很多地方读到我需要删除和重新创建远程,但实际上我的正常推送命令与上面的克隆完全相同,远程没有改变:

$ git push https://github.com/user-or-organisation/myrepo.git
Username: <my-username>
Password: <my-personal-access-token>

(@YMHuang用文档链接让我找到了正确的方向。)

生成令牌

使用创建个人访问令牌中的说明生成令牌。 (GitHub配置文件->设置->开发者设置->个人访问令牌)

实际上使用令牌

如果您已经在本地克隆了存储库

git remote remove origin
git remote add origin https://[TOKEN]@github.com/[REPO-OWNER]/[REPO-NAME]
git push

如果您正在克隆一个新的存储库

git clone https://[TOKEN]@github.com/[REPO-OWNER]/[REPO-NAME]

(将方括号和方括号之间的内容替换为相应的详细信息。@后面的部分与不带https://)的存储库url相同

克隆你的项目-> git克隆https://token@github.com//project.git 在项目文件夹-> git配置-global credential。辅助缓存

和工作

因为我使用的是macOS,所以我可以专门为macOS回答这个问题。 我们也可以在Windows上使用这个。它的工作原理! 在你的GitHub帐户中,点击右上角的头像,进入设置配置文件。

点击设置:

点击开发者设置:

点击个人访问令牌:

然后点击Generate new token:

为您的访问令牌提供一个名称,并检查第一个选项。

向下滚动并单击generate token

现在,当你推送repo时,使用以下语法:

git remote add origin https:<access__token>://@github.com/<username>/<repo__name>.git

git push https://<access__token>@github.com/<username>/<repo__name>.git

在我看来,你可以使用第二种选择,同时按下回购提供访问令牌,你就可以了。