Configuring Git Bash and SSH for GitHub
GitHub has excellent documentation for adding an existing project, which can be briefly summarized as follows:
- Create a new repository on Github
- Initialize a local repo, add files, and commit (git init,git add ., andgit commit -m "Commit message"respectively)
- Add the Github repo as a remote (git remote add origin [repo_url]
- Push changes to Github (git push origin master)
When pushing code, you will be prompted to enter your GitHub username and password. If you push code often or have a complex password/passphrase (as you probably should), this will quickly get tedious.
An alternative is to use SSH keys and configure an authentication agent to avoid having to re-enter credentials. Git Bash can be configured to automatically run ssh-agent. Add the following into your ~/.bashrc file (or create a new one if it doesn’t exist):
# Note: ~/.ssh/environment should not be used, as it
#       already has a different purpose in SSH.
env=~/.ssh/agent.env
# Note: Don't bother checking SSH_AGENT_PID. It's not used
#       by SSH itself, and it might even be incorrect
#       (for example, when using agent-forwarding over SSH).
agent_is_running() {
    if [ "$SSH_AUTH_SOCK" ]; then
        # ssh-add returns:
        #   0 = agent running, has keys
        #   1 = agent running, no keys
        #   2 = agent not running
        ssh-add -l >/dev/null 2>&1 || [ $? -eq 1 ]
    else
        false
    fi
}
agent_has_keys() {
    ssh-add -l >/dev/null 2>&1
}
agent_load_env() {
    . "$env" >/dev/null
}
agent_start() {
    (umask 077; ssh-agent >"$env")
    . "$env" >/dev/null
}
if ! agent_is_running; then
    agent_load_env
fi
# if your keys are not stored in ~/.ssh/id_rsa or ~/.ssh/id_dsa, you'll need
# to paste the proper path after ssh-add
if ! agent_is_running; then
    agent_start
    ssh-add
elif ! agent_has_keys; then
    ssh-add
fi
unset env
My GitHub SSH key is named github_rsa, so I had to change the two lines in the last if-statement from ssh-add to ssh add ~/.ssh/github\_rsa.
Now, when you run Git Bash, ssh-agent will start automatically and you will be able to git push and git pull from GitHub without needing to enter any credentials.
