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


当前回答

通常我是这样做的:

 git push https://$(git_token)@github.com/user_name/repo_name.git

git_token是从Azure DevOps中的变量配置中读取的。

你可以在这里阅读我的完整博客文章。

其他回答

您可以轻松地更改远程认证,首先:

删除当前原点:

git remote remove origin

然后:

git remote add origin https://<TOKEN>@github.com/<USERNAME>/<REPO>.git

你可以在这里找到如何生成你的身份验证令牌。

生成令牌

使用创建个人访问令牌中的说明生成令牌。 (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相同

在Android Studio中选择vcs→push标签。弹出窗口将显示用户名和密码。输入您的用户名,而不是密码,输入令牌号码。它会被推送到存储库。

这可以通过使用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 

是充分的。

自动化/使用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/