Building from @patthoyts's high-voted answer:
His answer uses but doesn't explain local
vs. global
vs. system
configs. The official git documentation for them is here and worth reading.
For example, I'm on Linux, and don't use a system config, so I never use a --system
flag, but do commonly need to differentiate between --local
and --global
configs.
My use case is I've got two Github crendentials; one for work, and one for play.
Here's how I would handle the problem:
$ 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://[email protected]':
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
It's also worth noting that there are ways to avoid this problem altogether, for example, you can use ~/.ssh/config
's with associated SSH keys for Github (one for work, one for play) and correspondingly custom-named remote hosts to solve authentication contextualizing too.
~/.netrc
file? – Mcclendonnetrc
, which would store multiple credential in an encrypted file. It is better than entering your password each time for each session, since the cache only "caches" the password for a certain time. See a full example here. – Bickfordrm ~/.git-credentials
afterwards. Being prompted for a password doesn't mean that they are not stored. – Sough