Git Credential Store
The git-credential-store helper in Git is a credential management feature that allows you to save Git credentials (such as usernames and personal access tokens) in a plain-text file. This file is often located in the home directory as ~/.git-credentials. The credential-store helper is useful when you want to avoid being prompted for credentials each time you access a Git repository. However, this method is less secure than using a credential manager like Git Credential Manager (GCM), as it stores credentials in plain text.
1. How the git-credential-store Helper Works
git-credential-store Helper WorksWhen configured, the
git-credential-storehelper saves credentials in a plain text file, so Git can retrieve them automatically when required for authentication.This setup is straightforward for single-user environments or systems where security restrictions are less stringent.
2. Configuring the git-credential-store Helper
git-credential-store HelperStep 1: Set up Git to use the
credential-storehelper.git config --global credential.helper storeThis command sets up Git to save credentials globally (for all repositories of the current user).
Step 2: After configuring, run any Git command that requires authentication, such as cloning a private repository:
git clone https://github.com/your-username/your-repository.gitGit will prompt you for your username and password (or personal access token). After entering your credentials, they will be stored in the
~/.git-credentialsfile, formatted as follows:https://username:[email protected]Example of
~/.git-credentials:https://user1:[email protected] https://user2:[email protected]Each line in this file represents a different set of credentials for a unique URL or repository host.
3. Configuring Multiple Credentials for Different Repositories
By default, the credential-store helper applies credentials globally, meaning it will try to use the same credentials for all repositories. To use different credentials for different repositories, you can specify credentials based on the repository’s URL in the ~/.git-credentials file.
Example Scenario
Let’s say you have two Git repositories:
Repository on GitHub:
https://github.com/user1/repo1Repository on GitLab:
https://gitlab.com/user2/repo2
To use different credentials for each repository, add both sets of credentials to ~/.git-credentials:
With this setup:
When you access
github.comrepositories, Git usesuser1's credentials.When you access
gitlab.comrepositories, Git usesuser2's credentials.
Configuration Steps
Manually Edit
~/.git-credentials:Open the
~/.git-credentialsfile in a text editor:Add entries for each set of credentials in the following format:
Example:
Ensure URL Specificity:
Git matches the URL specified in the
~/.git-credentialsfile with the repository URL you’re accessing.If you want more specific control, use the full URL for each repository:
4. Using Different Credentials for the Same Git Host
If you need to use different credentials for different repositories on the same host (e.g., multiple GitHub accounts), then specify the full URL path for each repository in ~/.git-credentials.
Example:
This way, Git can differentiate based on the repository URL, even if both repositories are hosted on the same domain.
5. Alternative: Configuring Credentials Per Repository Using Local Configuration
If you want to avoid adding credentials globally, you can configure credentials per repository using local Git configuration.
Set Up
credential.helper storeLocally:Navigate to the repository’s directory:
Set up the
credential-storehelper for this repository only:After running this command, the credentials you enter for this repository will be saved specifically for it, without affecting other repositories.
Test with Git Command:
Run any Git command that requires authentication (like
git pushorgit pull). Git will prompt for your credentials, which will be saved in the repository-specific configuration.
6. Security Considerations
Plain Text Storage: Since
~/.git-credentialsis stored in plain text, restrict permissions to secure the file:Using Alternative Credential Managers: For improved security, consider using Git Credential Manager (GCM) or SSH keys, as they offer encrypted credential storage.
Summary of Commands and File Setup
git config --global credential.helper store
Enables global credential storage in ~/.git-credentials.
~/.git-credentials
Stores credentials for specific hosts and repositories.
chmod 600 ~/.git-credentials
Restricts access to the credentials file for security.
git config credential.helper store
Sets credential storage for a specific repository (in local repo config).
This approach to managing Git credentials in ~/.git-credentials helps avoid repeated prompts while ensuring different credentials are used for different repositories.
Your example in ~/.git-credentials file is formatted correctly to store credentials for different Git hosts:
Explanation of the Format
Each line specifies a set of credentials for a specific host or repository. Here’s how it works:
Format:
https://username:token@hosthttps://: The protocol for Git operations over HTTPS.username: The Git username for the host (in this case,user1for GitHub anduser2for GitLab).token: The personal access token (PAT) for each user on their respective hosts (ghp_githubAccessTokenfor GitHub andglp_gitlabAccessTokenfor GitLab).host: The Git host URL (github.comfor GitHub andgitlab.comfor GitLab).
How Git Uses This File
When you perform a Git operation that requires authentication, Git will:
Check the
~/.git-credentialsfile for an entry that matches the URL you’re accessing.Use the matching username and token to authenticate without prompting for a password.
For example:
When you run a command like
git clone https://github.com/user1/repo1, Git will findhttps://user1:[email protected]in~/.git-credentialsand useuser1's credentials.When you run a command for GitLab, like
git clone https://gitlab.com/user2/repo2, it will usehttps://user2:[email protected].
Security Consideration
Since ~/.git-credentials stores credentials in plain text, limit its permissions for security:
This ensures that only your user can read or write to the file, keeping the credentials secure from other users on the system.
Last updated