跳到主要内容

2024-09-05

一言

哪有顷刻之间的心灰意冷,有的,只是日积月累的看透罢了。 --- 《觅楽-语录》 · 悟空


git从ssh变更为https access token

git remote set-url origin https://url

配置全局access token

git config credential.helper store
git config --global url."https://oauth2:<access token>@<git url>".insteadof "https://<git url>"

这个命令的意思是通过 git config 设置一个全局配置,当你在 Git 中使用 https://<git url> 作为远程仓库地址时,Git 会自动替换成 https://oauth2:<access token>@<git url>

以下是这个命令的具体含义:

  • git config --global:为 Git 配置全局选项(即对当前用户的所有仓库生效)。
  • url."https://oauth2:<access token>@<git url>".insteadof:这是配置项,用来告诉 Git 在遇到 https://<git url> 时,使用指定的 URL 替代它。
  • "https://<git url>":这是原始的 Git 远程仓库地址。
  • https://oauth2:<access token>@<git url>:这是替代后的 URL,其中 <access token> 应该是你的 GitLab 访问令牌。

这样,当你在某个 Git 仓库中使用 git pushgit clone 访问 https://<git url> 时,Git 会自动替换为 https://oauth2:<access token>@<git url>,并使用 OAuth 2.0 令牌进行身份验证,避免每次都输入用户名和密码。