所以我有一个组织给我的Mac,用于公司工作。我的 macOS 版本是 macOS Monterey 版本 12.6 现在我为我的公司设置了一个 github 帐户(启用 2FA 并使用带有个人访问令牌的 HTTPS) git 版本 2.37.0 (Apple Git-136) 在此处添加 .git/config 文件输出(存储库路径内的本地文件)。
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[remote "origin"]
url = https://github.com/<<company_name>>/<<repo_name>>.git
fetch = +refs/heads/*:refs/remotes/origin/*`
添加全局配置文件输出(~/.gitconfig)
[credential]
helper = osxkeychain
[url "https://company_username@github.com"]
钥匙串里面有两个 github.com 的条目
Internet password
application password
两者都有我的公司 ID 的个人访问令牌。 这一切都工作正常,因为 PAT 保存在钥匙串中。
现在我已经以我的名字创建了一个新的 github 帐户(用于个人项目),以下是本地 .git/config 文件输出。
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[user]
name = <<personal_username>>
email = <<personal_email_id>>
[remote "origin"]
url = https://<<PAT>>@github.com/<<personal_username>>/<<repo_name>>.git
fetch = +refs/heads/*:refs/remotes/origin/*
[credential]
helper =
我在这里添加了PAT,这样它就不会复制钥匙串中的PAT(因为我在网上尝试了不同的解决方案)
但是,当我尝试克隆或推送此存储库时,它会出现错误。
remote: Repository not found.
fatal: repository 'https://github.com/<<personal_user>>/<<repo>>.git/' not found
我的理解是,它是从钥匙串中获取 PAT,而该 PAT 是针对我公司的 PAT,因此出现此错误。不过我可能是错的。 为我的 .netrc 添加输出
machine github.com login <<company_username>> password <<company_PAT>>
我能想到两种方法来做到这一点
但是,在尝试了网上的所有内容后,我无法提出任何解决方案。 再说一遍,我正在使用 HTTPS (网上大部分解决方案都是.SSH) 预先感谢您。
https://<<PAT>>@github.com
:使用凭证助手时,不要放置令牌。
您输入您的 GitHub 用户帐户,即访问该存储库所需的帐户。
然后您在凭证助手中记录该用户的正确 PAT。
我建议安装 Microsoft 跨平台 GCM(Git Credential Manager),它将更新 osxkeychain。
然后记录您的个人用户帐户和关联的 PAT:
printf "host=github.com\nprotocol=https\nusername=you\npassword=PAT" | git credential-manager store
# replace 'you' and 'PAT' with your own GitHub account and token
使用
https://you@github.com/...
(而不是 https://company_username@github.com
)将强制 Git 从凭证助手中提取正确的令牌。
@VonC 感谢您上面的回答,这对我的第 2 步有所帮助。
最后,以下步骤对我有用。
第1步:git远程删除原点
第2步:git远程添加源https://<>@github.com/<
第3步:git推送
第4步:git push --set-upstream origin main