You only have to do this once per machine.
Why cache credentials?
As you have probably gathered by now, it will be annoying to enter your username and password each time you push changes to GitHub. It may even discourage you from pushing as frequently as you should. By storing your credentials on the computer, you won’t have to authenticate yourself manually each time you push to GitHub, and your credentials will be stored in a secure manner.
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 just setup Git with GitHub, you have a test repository. If you setup Git to work within RStudio, you have a test repository. If you already deleted those repositories, set one of them back up again.
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 these commands:
benjamin-laptop:Github benjamin $ git remote -v origin https://github.com/bensoltoff/myrepo (fetch) origin https://github.com/bensoltoff/myrepo (push) benjamin-laptop:Github benjamin $ git branch -vv * master b8e03e3 [origin/master] line added locally
We want to see that fetch and push are set to remote URLs that point to your GitHub repo. We also want to see that your local master branch has your GitHub master branch as upstream remote. Gibberish? Just check that your output looks similar to this.
Verify Git is up-to-date
In a shell, enter
git --version and verify that you have 1.7.10 or newer. If you don’t, update Git.
Turn on the credential helper
In the shell, enter
git config --global credential.helper wincred
Find out if the credential helper is already installed. In the shell, enter
git credential-osxkeychain. You should see something like this:
Usage: git credential-osxkeychain <get|store|erase>. If you do not, follow step 2 on the GitHub help page.
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 (that’s roughly 16 weeks).
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. From now on your “Push” button in RStudio will just work.
More options: SSH
Secure Shell (SSH) is an alternative method for authenticating trusted computers without using a password. There are some benefits to this approach over HTTPS, however it is generally more complicated to initially set up. If you wish to use this approach, see here for instructions on generating an SSH key and pairing it with your GitHub account.
- This page is derived in part from “UBC STAT 545A and 547M”, licensed under the CC BY-NC 3.0 Creative Commons License.
- “Chapter 10: Cache credentials for HTTPS” from Happy Git and GitHub for the useR