11 Cache credentials for HTTPS
If you plan to push/pull using HTTPS, you want Git to cache your credentials (username, password) (or you should set up SSH keys, chapter 12), so you don’t need to enter them over and over again. You’ll need to set this up on each computer you want to connect to GitHub from.
I do not explain all the shell and Git commands in detail. This is a black box diagnostic / configuration exercise.
11.1 Get a test repository
You need a functioning test Git repository. One that exists locally and remotely on GitHub, with the local repo tracking the remote.
If you have just verified that you can interact with GitHub (chapter 10) from your local computer, that test repo will be perfect.
If you have just verified that you can work with GitHub from RStudio (chapter 13), that test repo will also be perfect.
You may proceed when
- You have a test repo.
- You know where it lives on your local computer. Example:
- You know where it lives on GitHub. Example:
You know local is tracking remote. In a shell with working directory set to the local Git repo, enter:
git remote -v
Output like this confirms that fetch and push are set to remote URLs that point to your GitHub repo:
origin https://github.com/jennybc/myrepo (fetch) origin https://github.com/jennybc/myrepo (push)
git branch -vv
Here we confirm that the local
masterbranch has your GitHub master branch (
origin/master) as upstream remote. Gibberish? Just check that your output looks similar to mine:
master b8e03e3 [origin/master] line added locally
11.2 Verify that your Git is new enough to have a credential helper
In a shell, do:
11.3 Turn on the credential helper
In the shell, enter:
git config --global credential.helper wincred
22.214.171.124 Windows, plan B
If that doesn’t seem to work, install an external credential helper.
- Download the git-credential-winstore.exe application.
Run it! It should work if Git is in your
PATHenvironment variable. If not, go to the directory where you downloaded the application and run the following:
git-credential-winstore -i "C:\Program Files (x86)\Git\bin\git.exe"
Find out if the credential helper is already installed. In the shell, enter:
And look for this output:
usage: git credential-osxkeychain <get|store|erase>
If you don’t get this output, it means you need a more recent version of Git, either via command line developer tools or Homebrew. Go back to the Mac section of chapter (7).
Once you’ve confirmed you have the credential helper, enter:
git config --global credential.helper osxkeychain
In the shell, enter:
git config --global credential.helper 'cache --timeout=10000000'
to store your password for ten million seconds or around 16 weeks, enough for a semester.
11.3.1 Trigger a username / password challenge
Change a file in your local repo and commit it. Do that however you wish. Here are shell commands that will work:
echo "adding a line" >> README.md git add -A git commit -m "A commit from my local computer"
git push -u origin master
One last time you will be asked for your username and password, which hopefully will be cached.
Now push AGAIN.
You should NOT be asked for your username and password, instead you should see
Rejoice and close the shell.