【问题标题】:How to add a GitHub personal access token to Visual Studio Code如何将 GitHub 个人访问令牌添加到 Visual Studio Code
【发布时间】:2021-05-19 17:05:46
【问题描述】:

我收到an email 说 GitHub 将在 2021 年 8 月 13 日之后要求进行令牌身份验证。我想确保在此日期之后我不会出现服务中断(推/拉)。 所以我登录了 GitHub 和 created a token 作为我的单一存储库。

现在我想在 Visual Studio Code 中使用令牌从 GitHub 推送/拉取我的存储库,它使用 Git 和命令行,我已经安装在我的 Mac 上。

如何使用我刚刚创建的生成的令牌添加/替换来自 GitHub 的密码以从我的存储库推/拉?我可以从 Visual Studio Code 执行它还是从终端命令行添加它?

【问题讨论】:

    标签: git github visual-studio-code access-token


    【解决方案1】:

    按照这些简单的步骤使用personal access token 设置 GitHub 身份验证:

    1. 在 PC 上打开命令行窗口或在 Mac 上打开终端

    2. 将当前目录设置为项目根目录

      cd C:\Users\Giddy\source\repo\MySampleProject
      
    3. 运行命令以通过令牌设置远程访问

      git remote set-url origin https://username:token@github.com/username/repository.git
      

      例子:

      git remote set-url origin https://sampleuser:a7b19929***58fe65d1c@github.com/sampleuser/sampleproject.git
      

    【讨论】:

    • 在 repo 强制执行 2FA 后,这让 GitLab 身份验证为我工作。
    • git remote set-url origin https://username:token@github.com/username/repository.git 用于故障排除git push 工作得很好。 +1
    • 我这样做了,它似乎有效,但这也意味着令牌以纯文本形式存储在 git 配置文件中。这肯定不是一个好主意吗?
    【解决方案2】:

    在 Visual Studio Code (Mac) 上测试

    不需要额外的扩展。我只相信官方扩展,对不起。 KnisterPeter 的 GitHub 扩展

    1. 从 github.com 生成 personal access token
    2. 确保保存您的访问令牌(例如 ghp_pVC*****)
    3. 使用 Visual Studio Code 打开您的项目或在终端导航到您的项目,cd ~/path/to/your/project
    4. 在 Visual Studio Code 终端中,git remote set-url origin https://<personal_access_token>@github.com/<your_username or organization_name>/<repo_name>.git
    5. 现在你可以试试git push

    注意:

    生成个人访问令牌时,请确保启用工作流程

    提示

    您可以输入git remote -v 以查看您的来源或上游。

    origin  https://github.com/<username>/<repo_name>.git (fetch)
    origin  https://github.com/<username>/<repo_name>.git (push)
    upstream        https://github.com/<username>/<repo_name>.git (fetch)
    upstream        https://github.com/<username>/<repo_name>.git (push)
    

    同样在设置git remote set-url origin https://&lt;personal_access_token&gt;@github.com/&lt;your_username&gt;/&lt;repo_name&gt;.git之后

    你的git remote -v 应该是这样的:

    origin  https://<your_personal_access_token>@github.com/<username>/<repo_name>.git (fetch)
    origin  https://<your_personal_access_token>@github.com/<username>/<repo_name>.git (push)
    

    【讨论】:

    • 好的答案@sultanmyrza 谢谢!只需在第 3 步之前添加,您必须确保您位于包含您要提交的文件的目录中(酌情使用 cd 命令)
    • @JoseRondon 注意到,更新了?
    • 这应该是公认的答案。谢谢!
    • 太好了,谢谢?
    【解决方案3】:

    如果您使用GitHub extension by KnisterPeter 收到这样的消息:

    要启用 Visual Studio Code GitHub 支持,请设置个人访问令牌

    1. 转到设置开发者设置个人访问令牌

      或者直接去https://github.com/settings/tokens

    2. 点击生成新令牌

    3. 回到 Visual Studio Code 并打开 Command Palette (Ctrl + Shift + P)

    4. 输入GitHub:设置个人访问令牌

    5. 粘贴新生成的令牌中的值

    【讨论】:

    • 搜索“Github:设置个人访问令牌”时“没有匹配的命令”
    • @AmarIlindra - 你需要安装 Github extension - 如果你安装了它,但仍然看不到它,我会在那里提出问题
    • @KyleMit 这不是官方扩展
    • @mr.loop,当然,但这是一个常见的问题,因为它有相同的调试错误消息,所以无论如何都可能将人们带到这个 Q&A 中
    • 不信任第三方使用我的访问令牌。这只是糟糕的形式。
    【解决方案4】:
    1. 在 Visual Studio Code 中,尝试从远程存储库推入或拉出
    2. 单击 Visual Studio Code 底部的“登录 GitHub”消息
    3. 在提示中点击“允许”
    4. 在 Visual Studio Code 顶部显示的提示中输入令牌

    查看与 GitHub 身份验证相关的输出:

    1. 在 Visual Studio Code 中打开终端
    2. 点击终端面板顶部的“输出”
    3. 从下拉菜单中单击“GitHub 身份验证”

    在撰写本文时,我使用的是 Visual Studio Code v1.56.0。

    【讨论】:

    • 在第一步之后有不同的流程,但总体上相似(VSCode v1.56.1,Windows)。当它真的想要令牌时,被要求输入“密码”的身份验证提示弄糊涂了(我一开始输入了我的 GitHub 密码)
    • 我没有收到这样的提示
    • 当我尝试“git push”时,我从未看到“登录 GitHub”消息,我看到的只是“远程:2021 年 8 月 13 日删除了对密码身份验证的支持。请使用个人访问权限远程令牌:请参阅github.blog/… 了解更多信息。致命:无法访问'github.com/ivanpartizan/algorithms.git':请求的URL返回错误:403'我创建了我的个人访问令牌,但我该如何使用它???
    • @ivanmilenkovic 看到:stackoverflow.com/a/66830126/7896203 它为我解决了这个问题
    【解决方案5】:

    如果系统未提示您输入用户名和密码,您的凭据可能会缓存在您的计算机上。您可以更新钥匙串中的凭据,用令牌替换旧密码。

    Updating credentials from the macOS Keychain

    【讨论】:

    • 这正是我的问题。关闭 VS Code,删除所有 github 登录凭据,然后重新启动 VS Code 为我解决了这个问题。我只需要重新登录以打开设置同步,其余的都由 VS Code 处理
    【解决方案6】:

    我在进行 GitHub 密码验证时,遇到了 Visual Studio Code 的“你不能再对 GitHub 使用密码验证”的问题。

    我在 Mac 上,所以我有股票 Git (v2.21)。

    我尝试了这些步骤,现在可以通过 Visual Studio Code 无错误地签入代码(注意此方法使用 Microsoft Git Credential Manager Core - 请参阅 here):

    1. 使用Homebrewbrew install git 更新Git。它将在您的系统上创建一个指向 Git 的新符号链接,指向 /usr/local/bin/git,因此不会使用 Mac 库存 Git
    2. 使用instructions on GitHub 设置个人访问令牌
    3. 进入 Mac 钥匙串并删除 vscode.github.com 密码凭证
    4. 安装 Microsoft 的 Git Credential Manager Core (GCMC) - instructions on GitHub
    5. 退出并重新启动 Visual Studio Code,然后再次推送。系统将提示您在浏览器中授权 GCMC。一旦你这样做了,从 Visual Studio Code 推送应该会自动工作,因为 GCMC 将继续处理 Git 授权。

    【讨论】:

      【解决方案7】:

      复制以下行并将其粘贴到您的 Visual Studio Code 终端中

      git remote set-url origin https://<TOKEN>@github.com/<USERNAME>/<REPOSITORYNAME>.git
      

      将 TOKEN 替换为您的令牌 USERNAME 使用用户名,将 REPOSITORYNAME 替换为您的存储库。这应该有效。 您的最后一行应该类似于:

      git remote set-url origin https://asdlkuresdflj23433423lj234@github.com/brother/sampleapp.git
      

      【讨论】:

      • 当然使用 VSCode 1.62.2 为我工作,但该令牌存储在哪里?希望不是纯文本。
      【解决方案8】:

      如果您为远程存储库使用 HTTPS URL,这意味着您可以缓存您的凭据:今天,这将是您的 GitHub 用户帐户名和密码,明天,密码将是您的令牌。

      检查您的git config credential.helper 结果。 如果是 manager 或 manager-core,请使用 git credential-manager-core erase 删除旧密码
      (“破坏性命令”,它将远程访问 github.com 条目及其关联值,即密码)
      git credential-manager-core get读取那个旧值)

      printf "protocol=https\nhost=github.com\nusername=<yourGitHubAccountName>"|git credential-manager-core erase
      

      然后git credential-manager-core store 存储令牌:

      printf "protocol=https\nhost=github.com\nusername=<yourGitHubAccountName>\npassword=<newToken>"|git credential-manager-core store
      

      (如果凭证助手是 manager 而不是 manager-core,则将 credential-manager-core 替换为 credential-manager

      Visual Studio Code 将使用该凭据助手,并为远程站点存储新的“密码”(令牌)。


      如果你得到了,在 Linux 上:

      git: 'credential-manager-core' is not a git command. See 'git --help'. 
      

      您需要download and install GCM

      【讨论】:

      • 我运行了你的命令,我看到了'osxkeychain'
      • @user1186050 对于 Mac,您可以将 credential-manager-core 替换为 credential-osxkeychain (stackoverflow.com/a/28007050/6309) 或者您可以以图形方式进行:docs.github.com/en/github/using-git/…
      • 我跑了git config credential.helper 却一无所获..
      • @BenWhitmore 你使用的是什么版本的 Git 和操作系统?
      • @VonC git 2.25.1 在 Ubuntu 20.04.2 LTS 上
      【解决方案9】:

      我遇到了同样的问题,最后出现“登录 GitHub”提示并成功验证。基本上你在得到下面的确认页面后需要做什么(在你点击浏览器窗口中的'Authorize Visual Studio Code'之后)

      成功了!

      授权成功。您将被重定向回 Visual 工作室代码

      没用?如果您没有被重定向,您可以手动添加令牌。

              **Your authorization token:**
      

      vscode://vscode.github-authentication/did-authenticate?windowid=1&code=[Not-A+Real+Token)SJ###AASDf

      …………等等


      正在切换回 VS Code(可能已经自动成为焦点) 并选择 “不再提示” 或任何消息... 然后你终于可以看到并点击提示'Signing into GitHub ...'

      希望这对某人有所帮助......因为听起来很简单,当关闭提示或选择“显示 Git 日志”选项时,VS Code 不会提示。

      【讨论】:

      • 请解释“终于出现'Signing into GitHub'提示”。显然,您对此有困难,应该详细说明。
      • 我的问题与 OP 的问题相同。
      • 如何出现“登录 GitHub”提示?这是这个答案的结论还是你首先取得的成就?如果是后者,如何?请通过editing (changing) your answer 回复,而不是在 cmets 中(without "Edit:"、"Update:" 或类似的 - 答案应该看起来像是今天写的)。
      猜你喜欢
      • 2021-10-17
      • 1970-01-01
      • 2021-12-12
      • 2023-01-28
      • 2021-09-04
      • 2021-12-26
      • 1970-01-01
      • 2022-11-09
      • 2020-01-06
      相关资源
      最近更新 更多