Reputation: 369
I know it's a bit of a pain to do, but I have multiple accounts for my personal accounts and work accounts. I generate tokens for these accounts so they have restricted access and I don't have to use my actual password so it is more secure. The problem I have is that there doesn't seem to be a very good way to store these passwords for multiple accounts. This applies for both Windows and Linux. The "easiest" way I found was just to have multiple user accounts on the system, which isn't that convenient. Otherwise if you have one repo and you try and set the user using git config user.name
etc, it'll use the wrong password/account anyways for that repo.
Is there an easier or better way to go around this? To have multiple passwords saved for git and just be able to choose the account you want to use for the repo and have it just work?
Upvotes: 7
Views: 5429
Reputation: 1323553
You might consider using a credential helper like the recent (July 2020) GCM Core, announced by GitHub.
Git Credential Manager Core (GCM Core) is a secure Git credential helper built on .NET Core that runs on Windows and macOS.
Linux's support is planned, but not yet scheduled.
For Linux, in the meantime, you can use the Java GCM.
(see last section below)
In both instance, the usage is the same: you can cache credentials (like your token) for a given URL.
Update Sept. 2020 (2+ months later) for Linux: microsoft/Git-Credential-Manager-Core
issue 135 mentions, From GitHubber Matthew John Cheetham:
We have a pre-release of a GCM Core that supports Linux! 🥳
At the moment we provide a Debian package, and a tarball of the
git-credential-manager-core
single binary.
The.deb
is currently unsigned and not uploaded anywhere except on GitHub.
We are in the process of getting package signing set up and will be publishing it to an official Microsoft feed (so you'll be able to useapt-get
!).
Currently, the pre-built binaries are only provided for 64-bit Intel processors.
See more at Credential stores on Linux
There are currently three options for storing credentials that Git Credential Manager Core (GCM Core) manages on Linux platforms:
- freedesktop.org Secret Service API
- GPG/pass compatible files
- Plaintext files
Update Aug. 2021: issue 135 is now closed by Matthew John Cheetham (GitHub staff):
Closing this issue as GCM Core can now run on Linux distributions.
For further issues/bugs, or support for more distributions, please open new issues. Thanks!
GCM 2.2.0 (July 2023) adds login
, logout
and list
commands for the GitHub provider, as well as UI and TTY prompts to select between GitHub user account.
Upvotes: 5
Reputation: 201
Most common way people solve this issue is to: git config --global credential.useHttpPath true
- and have different user/pass record in credential storage for every repo. But if you have tons of repos, this can pollute credential store quite fast, making it not really that nice of a solution.
You would end up with records like:
git:https://[email protected]/YourUsername/YourRepo1.git
git:https://[email protected]/YourUsername/YourRepo2.git
git:https://[email protected]/YourUsername/YourRepo3.git
git:https://github.com/YourUsername/YourRepo1.git
git:https://github.com/YourUsername/YourRepo2.git
git:https://github.com/YourUsername/YourRepo3.git
The way I prefer to solve this issue is to use credential.namespace git config option. Idea is to create different namespaces for your different accounts, so that they don't clash in your system credential manager.
For example (using github.com as example url), you can decide one of those accounts is your main and set it up in global git config:
git config --global credential.namespace "MainAccount"
git credential approve
url=https://github.com
username=YourMainAccUsername
password=YourMainAccPasswordOrToken
<Enter>
and then enter repo where you want to log in with different credentials and set it up locally:
cd path/to/other/repo
git config credential.namespace "OtherAccount"
git credential approve
url=https://github.com
username=YourOtherAccUsername
password=YourOtherAccPasswordOrToken
<Enter>
And you can verify in your credential manager, you should have two new records:
MainAccount:https://github.com/
OtherAccount:https://github.com/
And you can verify in command prompt what credentials will be sent to git server:
git credential fill
url=https://github.com
<Enter>
Upvotes: 14