如何将 github 个人访问令牌添加到 Visual Studio 代码

use*_*050 22 git github access-token visual-studio-code

我收到一封电子邮件,说 Github 将在 2021 年 8 月 13 日之后要求进行令牌身份验证。我想确保在此日期之后我不会中断服务(推/拉)。所以我登录到 Github 并为我的单个 repo创建了一个令牌

现在我想在 Visual Studio Code 中使用令牌从 Github 推/拉我的 repo,它使用 git 和命令行,我已经安装在我的 Mac 上。

问题- 我该怎么做才能使用我刚刚创建的生成的令牌添加/替换来自 Github 的密码以从我的存储库中推送/拉取?我可以从 Visual Studio Code 执行它还是从终端命令行添加它?

sul*_*rza 59

在 Visual Studio Code (Mac) 上测试

不需要额外的扩展。我只信任官方扩展,抱歉。KnisterPeter 的 GitHub 扩展

  1. 从 github.com生成个人访问令牌
  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)
Run Code Online (Sandbox Code Playgroud)

设置后也是如此git remote set-url origin https://<personal_access_token>@github.com/<your_username>/<repo_name>.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)
Run Code Online (Sandbox Code Playgroud)


Gid*_*aya 41

按照以下简单步骤使用个人访问令牌设置 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

  • 我这样做了,它似乎有效,但这也意味着令牌以纯文本形式存储在 git 配置文件中。这当然不是一个好主意吗? (5认同)
  • 在仓库强制执行 2FA 后,GitLab 身份验证对我有用。 (3认同)
  • “git Remote set-url origin https://username:token@github.com/username/repository.git”用于解决“git Push”问题非常有效。+1 (2认同)

Kyl*_*Mit 17

如果您使用KnisterPeter 的 GitHub 扩展收到类似这样的消息:

\n
\n

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

\n

启用 Visual Studio Code GitHub 支持

\n
\n
    \n
  1. 转到设置\xe2\x86\x92开发人员设置\xe2\x86\x92个人访问令牌

    \n

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

    \n
  2. \n
  3. 单击生成新令牌

    \n

    设置 -> 生成新令牌

    \n
  4. \n
  5. 返回 Visual Studio Code 并打开命令面板( Ctrl++ Shift)P

    \n
  6. \n
  7. 输入GitHub:设置个人访问令牌

    \n

    设置个人访问令牌

    \n
  8. \n
  9. 粘贴新生成的令牌中的值

    \n
  10. \n
\n

  • 搜索“Github:设置个人访问令牌”时“没有匹配的命令” (16认同)
  • 不信任第三方使用我的访问令牌。这只是糟糕的形式。 (6认同)
  • @KyleMit 这不是官方扩展 (4认同)
  • @AmarIlindra - 你需要安装 [Github 扩展](https://marketplace.visualstudio.com/items?itemName=KnisterPeter.vscode-github) - 如果你安装了它,但仍然没有看到它,我在那里提出问题 (3认同)

Chr*_*ard 5

  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。

  • 当我尝试“git Push”时,我从未看到“登录 GitHub”消息,我看到的只是“远程:对密码身份验证的支持已于 2021 年 8 月 13 日删除。请使用个人访问令牌远程:请参阅 https:// /github.blog/2020-12-15-token-authentication-requirements-for-git-operations/ 了解更多信息。致命:无法访问 'https://github.com/ivanpartizan/algorithms.git/':请求的 URL 返回错误:403' 我创建了我的个人访问令牌,但如何使用它??? (8认同)
  • 第一步之后的流程有所不同,但总体相似(VSCode v1.56.1,Windows)。尽管确实需要令牌,但对要求输入“密码”的身份验证提示感到困惑(我首先输入了 GitHub 密码) (3认同)
  • 我没有收到这样的提示 (2认同)

小智 5

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

git remote set-url origin https://<TOKEN>@github.com/<USERNAME>/<REPOSITORYNAME>.git
Run Code Online (Sandbox Code Playgroud)

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

git remote set-url origin https://asdlkuresdflj23433423lj234@github.com/brother/sampleapp.git
Run Code Online (Sandbox Code Playgroud)


Von*_*onC 4

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

检查你的git config credential.helper结果。如果它是管理器或管理器核心,请使用git credential-manager-core erase
(“破坏性命令”,因为它将远程该github.com条目及其关联值,密码)删除旧密码
(agit credential-manager-core get读取该旧值)

printf "protocol=https\nhost=github.com\nusername=<yourGitHubAccountName>"|git credential-manager-core erase
Run Code Online (Sandbox Code Playgroud)

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

printf "protocol=https\nhost=github.com\nusername=<yourGitHubAccountName>\npassword=<newToken>"|git credential-manager-core store
Run Code Online (Sandbox Code Playgroud)

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

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


如果您在 Linux 上得到:

git: 'credential-manager-core' is not a git command. See 'git --help'. 
Run Code Online (Sandbox Code Playgroud)

您需要下载并安装 GCM

  • @user1186050 对于 Mac,您可以将 `credential-manager-core` 替换为 `credential-osxkeychain` (/sf/answers/1960493531/) 或者您可以以图形方式执行此操作:https://docs.github .com/en/github/using-git/updating-credentials-from-the-macos-keychaina (2认同)