我正在尝试使用个人访问令牌与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/'认证失败"


当前回答

唯一的答案,帮助我在一个VS Code环境和一个私人GitHub。

First =>在这里生成令牌:https://github.com/settings/tokens。 Second =>在VS Code终端的正确位置执行“cd”。 Third =>如下所示:

git克隆https://[MY_USER_NAME]:[GITHUB_PERSONAL_ACCESS_TOKEN]@github.com/davidsonlima/davidson-myrepo.git不带方括号

其他回答

首先,需要创建一个个人访问令牌(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用文档链接让我找到了正确的方向。)

这对我使用ssh工作:

设置→开发人员设置→生成新的令牌。

git remote set-url origin https://[APPLICATION]:[NEW TOKEN]@github.com/[ORGANISATION]/[REPO].git

这可以通过使用github部署键来完成,它可以缩小对单个github回购的访问权限,以及使写权限可选。

Github部署密钥使用用户生成的ssh密钥,使用ssh-keygen创建一个私钥文件和一个公钥文件。

假设给定ssh-keygen的密钥名是key-test,私有和公共文件位于~/中。Ssh /key-test和~/. Ssh /key-test。酒吧。

假设github项目名为keytest。

要通过github项目网页向项目添加部署密钥,获取设置/部署密钥并单击添加。粘贴公钥文件的内容~/.ssh/key-test。进入目标框并确认。

修改~/的内容。Ssh /config文件包括以下内容:

Host gh-keytest
        Hostname github.com
        IdentityFile=/home/user/.ssh/key-test

注意:gh-keytest是一个任意的别名。

现在你可以使用了

git push git@gh-keytest:<githubaccountname>/keytest.git

只用推就能做到

git remote remove origin  # in case origin is already set
git remote add origin git@gh-keytest:<githubaccountname>/testscope.git
git push --set-upstream origin main

注意:用正确的分支名称替换main。

之后

git push 

是充分的。

在与这个问题斗争了差不多一整天之后,在ORG/REPO部分硬编码到我们的构建脚本中得到了可怕的“远程未找到”错误,最终我通过使用TRAVIS_REPO_SLUG找到了一个有效的解决方案。

将此转换为硬编码属性立即起作用。

git remote set-url origin https://[ORG]:${TOKEN}@github.com/${TRAVIS_REPO_SLUG}

从开发人员设置生成访问令牌后,运行这些命令, git push origin [branch]用户名为'https://github.com': [accessToken]密码为'https://[accessToken]@github.com':[accessToken]