Git CLI Save Username and Password
With Github moving to key based authentication, in my opinion, it has become less convenient using the Git command line for pushes and pulls to repositories that require authentication. However Git has a way of storing your credentials on the system so you dont need to enter your username and key each time to pull or push from a repository. In this post I will demonstrate how to have Git cache your credentials so you do not have to keep finding and referencing your key.
Before continuing you need to be aware that it stores your username and key in a plain text file in your users home directory
~/.git-credentials. So needless to say this is not secure and you need to make sure that you are willing to accept the risk of doing this in your environment because anyone that can read that file can push and pull to your git repository as your user.
Instruct Git to Cache Credentials
All you need to do is issue the command below.
$ git config --global credential.helper store $
And now the next pull/push that requires authentication it will store those credentials, so do a pull or push that will require an authentication.
$ git pull Username for 'https://github.com': myuser Password for 'https://firstname.lastname@example.org': remote: Enumerating objects: 10, done. remote: Counting objects: 100% (10/10), done. remote: Compressing objects: 100% (2/2), done. remote: Total 6 (delta 4), reused 6 (delta 4), pack-reused 0 Unpacking objects: 100% (6/6), 2.05 KiB | 525.00 KiB/s, done. ~ Output Omitted ~ $
And now to test you can do another pull or push and it will not ask for authentication.
$ git pull remote: Enumerating objects: 10, done. remote: Counting objects: 100% (10/10), done. remote: Compressing objects: 100% (2/2), done. remote: Total 6 (delta 4), reused 6 (delta 4), pack-reused 0 Unpacking objects: 100% (6/6), 2.05 KiB | 525.00 KiB/s, done. ~ Output Omitted ~ $
View or Update Stored Credentials
As I pointed out earlier, your credentials will be cached in your home directory so to view your stored credentials you can issue the command below.
$ cat ~/.git-credentials https://myuser:ghp_fvrShDFHDTydfgB$@%ydygHe4Hdfgu^ADf@github.com
So if you ever need to update the username or key you can modify this file. And again make sure that only the owner has the ability to read and write and the group and everyone has zero rights to the file, not able to read, write, or execute.
$ls -alh ~/.git-credentials -rw------- 1 myuser myuser 71 Jan 15 08:32 /home/myuser/.git-credentials $