Post

Configure Git and SSH for Github

Configure Git and SSH for Github

This article will guide you through the process of setting up your git credentials on your computer, creating a ssh key, and adding your key to Github. You’ll also need a GitHub account and git installed on your local machine. If you haven’t done this yet, head over to the GitHub website and follow their instructions to get started.

Setting Your Credentials in Git

You can change the credentials associated with your git commits using the git config command. The credentials you set will be visible in any future commits you push to GitHub from the command line.

Globally

Setting your git credentials for every repository on your computer:

1
2
git config --global user.name "your username"
git config --global user.email "[email protected]"

Singular

Setting your git credentials for a single repository:

Navigate to the current working directory to the local repository where you want to configure the credentials that are associated with your git commits:

1
2
git config user.name "your username"
git config user.email "[email protected]"

You can verify your modifications with the command for either option above:

1
git config --list

Using SSH Keys With GitHub

GitHub no longer accepts password authentication for command-line access. You need to authenticate via a personal access token or use an SSH key. SSH keys consist of two parts: a public key and a private key. The public key is shared with the server (GitHub), while the private key remains on your local machine.

Create Your SSH Keys

To generate a new SSH key pair, follow these steps:

1
ssh-keygen -t rsa -b 4096 -C "[your github email address]"

After typing the command into your terminal, you’ll need to specify the file where the keys will be saved. By default, it’s located in your home directory, in a hidden folder named .ssh, but you can change it as needed.

You’ll then be asked for a passphrase to add to your key pair, adding an extra layer of security. It’s not mandatory, but it’s always recommended.

Then you’ll be asked for a passphrase to add to your key pair. This adds an extra layer of security if, at any time, your device is compromised. It is not obligatory to add a passphrase, but it’s always recommended.

You can list your keys using the following command:

1
ls -al ~/.ssh

Add SSH Key to ssh-agent

The ssh-agent program runs in the background, holding your private keys and passphrases securely, and keeps them ready for use by SSH. It saves you from typing your passphrase every time you want to connect to a server.

1. Verify ssh-agent is running in the background

1
eval "$(ssh-agent -s)"

2. Add your SSH private key (the one without extension) to the ssh-agent.

1
ssh-add ~/.ssh/id_rsa

Add SSH Key to GitHub Account

The final step is to add your public key to your GitHub account. Follow these instructions:

1. Copy your SSH public key to your clipboard. You can open the file where it is located with a text editor and copy it, or use the terminal to show its contents.

1
cat ~/.ssh/id_rsa.pub

2. Log into your Github account. In the top right corner of any page, click your profile photo, then click Settings. 3. In the user settings sidebar, click on SSH and GPG keys. 4. Click on the New SSH key button. 5. Type the title and your SSH key, and press the Add SSH key button.

Now you have added your SSH key to your GitHub account.

Conclusion

Congratulations — you’ve learned how to connect to GitHub via SSH!

This post is licensed under CC BY 4.0 by the author.

Trending Tags