This article provides a guide for setting up Git on a machine and linking it to a GitHub account. It covers the installation of Git, the generation of SSH keys, and the addition of the public key to a GitHub account. The article also includes instructions for creating a test repository on GitHub and pushing a change to it.
Introduction
It is important to set up a version control system like Git to help you track and manage your code changes. Git is a popular choice for version control and is widely used by developers and organizations around the world. In this guide, we’ll walk you through the steps of installing Git on your machine and setting it up to work with your GitHub account.
Prerequisites
Before you can set up Git, you’ll need to have the following:
- A GitHub account (sign up for one at github.com)
- The Windows Subsystem for Linux (WSL) with the Ubuntu 20.04 distribution is installed on your machine.
Install Git
To install Git on your machine, open a terminal or command prompt and type the following command:
sudo apt-get install git
This will install Git on your machine. You may be prompted to enter your password to complete the installation.
Configuration
Once Git is installed, you’ll need to configure it with your username and email address. This will help Git identify you as the author of any changes you make to your code. To set your username and email, type the following commands in your terminal or command prompt:
git config --global user.name "Your Name"
git config --global user.email "youremail@domain.com"
Set Up SSH Keys
In order to securely connect to your GitHub repository from your local machine, you’ll need to set up Secure Shell (SSH) keys. SSH keys are a pair of unique cryptographic keys that are used to authenticate your connection to GitHub.
- In your terminal or command prompt, type the following command to generate an SSH key:
ssh-keygen -t ed25519 -C "youremail@example.com"
- Press Enter to accept the default file location for the key.
- Type a passphrase for your SSH key and press Enter. Make sure to choose a strong, unique passphrase that you will remember.
- Type the passphrase again to confirm it and press Enter.
- Type the following command to view your public key:
cat ~/.ssh/id_ed25519.pub
- Select and copy the entire output.
- Go to your GitHub account settings and click on the “SSH and GPG keys” tab.
- Click on the “New SSH key” button.
- Type a name for your key in the “Title” field (e.g. “My local machine”).
- Paste your public key into the “Key” field.
- Click on the “Add SSH key” button.
Test Your Connection
To test your connection to GitHub, type the following command in your terminal or command prompt:
ssh -T git@github.com
If you are prompted to “continue connecting,” type “yes” and press Enter. If your connection is successful, you should see a message saying “Hi username! You’ve successfully authenticated, but GitHub does not provide shell access.”
Create a Test Repository
Now that you have Git and SSH set up, you can create a test repository on GitHub and push a change to it.
- In the GitHub web interface, create a new repository by clicking on the “New” button in the top-right corner of the page.
- Type a name for your repository and click on the “Create repository” button.
- Follow the instructions provided by GitHub to push a change to your repository.
The following steps walk through an example of creating a repository and pushing an update over to GitHub
- In the terminal or command prompt, navigate to the directory where you want to create your repository.
- Type the following commands to initialize your repository, add a file, and commit your changes:
# Initialize the repository
git init
# Add a file to the repository
echo "# test" >> README.md
# Add the file to the staging area
git add README.md
# Commit the file to the repository
git commit -m "first commit"
- Type the following command to create a main branch for your repository:
git branch -M main
- Type the following command to link your repository to the one you created on GitHub:
git remote add origin git@github.com:adamfordyce11/test.git
- Type the following command to push your changes to the main branch of your repository on GitHub:
git push -u origin main
This will push your changes to the main branch of your repository on GitHub. You can verify that the changes were successful by checking the repository on the GitHub web interface.
Conclusion
Congratulations! You have successfully set up Git and linked it to your GitHub account. You are now ready to track and manage your code changes with Git. Make sure to keep your private SSH key and passphrase secure, as anyone with access to them will be able to access your repositories.