When using Git for version control, being prompted for username and password on every push can be quite cumbersome. To avoid this, we can use the following methods to simplify the process:
1. Using SSH Keys for Authentication
By configuring SSH keys, you can generate a public-private key pair locally and add the public key to the SSH keys section of your remote repository. This way, Git can authenticate using the key without requiring username and password on each push.
Steps to follow:
-
Generate SSH keys locally (if not already done):
bashssh-keygen -t rsa -b 4096 -C "your_email@example.com"Follow the prompts to generate the key pair.
-
Add the public key content (typically found in
~/.ssh/id_rsa.pub) to the SSH keys section of your GitHub, GitLab, or other Git server under your user settings. -
Ensure your remote repository URL uses SSH format instead of HTTPS. Check and modify it with:
bashgit remote -v git remote set-url origin git@github.com:username/repository.git
2. Using Credential Helpers
Git supports using credential helpers to cache username and password. This allows you to avoid re-entering credentials for a certain period (or permanently).
Steps to follow:
-
Enable Git's credential helper:
bashgit config --global credential.helper storeOr, use the
cacheoption to cache credentials for a certain time (default 15 minutes):bashgit config --global credential.helper cache -
Enter username and password on the first push; you won't need to re-enter them within the validity period.
3. Modifying the Global .gitconfig File
For users who want to avoid repeating configurations across multiple projects, directly modify the global .gitconfig file to add credential helper configuration.
File modification example:
ini[credential] helper = store