To set the Git username and password in the terminal, you can follow these steps. However, it is important to note that Git does not store passwords in plain text to protect your credentials. Instead, it uses a credential helper or can cache the password for HTTPS repositories.
For setting a password, follow these methods based on your requirements:
1. Using Credential Helper (Recommended)
You can use Git's credential helper to cache your credentials securely, so you do not have to enter the username and password every time.
bash
git config --global credential.helper cache
# Set the cache timeout (time in seconds)
git config --global credential.helper 'cache --timeout=3600' # Cache for 1 hour
2. Store Password in the Git Configuration
You can also store your username and password directly in the .gitconfig file for a specific repository. Be careful as this is less secure:
bash
git config --global credential.helper store
After you run this command, the next time you enter your credentials, they will be saved in your .git-credentials file in your home directory.
3. Using SSH Authentication
For better security, consider using SSH keys instead of HTTPS passwords. You can create an SSH key and add it to your GitHub or GitLab account. Here’s how to set up SSH: