📅  最后修改于: 2023-12-03 14:41:27.103000             🧑  作者: Mango
When using Git, you often need to interact with remote repositories that require authentication. By default, Git requires you to enter your credentials (username and password) every time you interact with a remote repository. However, this can become tedious, especially when working with multiple repositories.
Git provides a convenient way to remember your credentials so that you don't have to enter them every time. This feature is particularly useful when working with HTTPS URLs, as it helps automate the authentication process.
To remember your Git credentials, you can use the git config
command. Open the terminal and execute the following command:
git config --global credential.helper store
This command sets up Git to use the store
credential helper, which stores your credentials in a plain-text file on your computer. It remembers your credentials until you either explicitly remove them or clear the file.
Once you have run this command, Git will remember your username and password for future interactions with remote repositories.
In addition to the store
credential helper, Git provides a few other options. Here are two popular alternatives:
cache: The cache
credential helper caches your credentials in memory for a certain amount of time. This is useful when you want Git to remember your credentials for a limited period, reducing the risk of unauthorized access if someone else gains access to your computer.
To use the cache
credential helper, run the following command:
git config --global credential.helper cache
By default, Git will cache your credentials for 15 minutes. You can change the cache timeout by setting the credential.helper
configuration option in Git.
manager: The manager
credential helper provides a more secure way to store your credentials. It utilizes the native credential storage mechanisms of your operating system, such as macOS Keychain or Windows Credential Manager.
To use the manager
credential helper, run the following command:
git config --global credential.helper manager
This command configures Git to use the native credential storage mechanism, which provides better security for storing your credentials.
To remove or update your stored Git credentials, you have a few options:
Manually: If you used the store
credential helper, you can edit the plain-text file where your credentials are stored. Locate the file (usually ~/.git-credentials
on Unix-based systems) and update or remove the credentials as needed. Note that credentials are stored per repository.
Git command: You can also use Git commands to selectively remove or update credentials. For example, to clear the stored credentials for a specific repository, execute the following command inside the repository's directory:
git credential reject
This command will prompt Git to forget the stored credentials for the current repository. The next time you interact with that repository, Git will ask for your credentials again.
Remembering your Git credentials can significantly improve your productivity as a programmer. By automating the authentication process, you can focus more on your work and spend less time entering credentials.