从@patthoyts的最高答案(https://stackoverflow.com/a/15382950/4401322)构建:
他的答案使用但不解释“本地”与“全局”与“系统”配置。他们的官方git文档在这里,值得一读。
例如,我在Linux上,并且不使用系统配置,因此我从不使用--system
标志,但通常需要区分--local
和--global
配置。
我的用例是我有两个Github凭证;一种是工作,另一种是娱乐。
这是我处理问题的方法:
$ cd work
# do and commit work
$ git push origin develop
# Possibly prompted for credentials if I haven't configured my remotes to automate that.
# We're assuming that now I've stored my "work" credentials with git's credential helper.
$ cd ~/play
# do and commit play
$ git push origin develop
remote: Permission to whilei/specs.git denied to whilei.
fatal: unable to access 'https://github.com/workname/specs.git/': The requested URL returned error: 403
# So here's where it goes down:
$ git config --list | grep cred
credential.helper=store # One of these is for _local_
credential.helper=store # And one is for _global_
$ git config --global --unset credential.helper
$ git config --list | grep cred
credential.helper=store # My _local_ config still specifies 'store'
$ git config --unset credential.helper
$ git push origin develop
Username for 'https://github.com': whilei
Password for 'https://whilei@github.com':
Counting objects: 3, done.
Delta compression using up to 12 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 1.10 KiB | 1.10 MiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/whilei/specs.git
b2ca528..f64f065 master -> master
# Now let's turn credential-helping back on:
$ git config --global credential.helper "store"
$ git config credential.helper "store"
$ git config --list | grep cred
credential.helper=store # Put it back the way it was.
credential.helper=store
还值得注意的是,有一些方法可以完全避免此问题,例如,您可以将~/.ssh/config
Github的SSH密钥(带有一个用于工作,一个用于播放)以及相应的自定义远程主机使用SSH来解决身份验证上下文。
~/.netrc
文件吗?