DEV Community

Rachel Muriuki
Rachel Muriuki

Posted on

Getting Started with Git and GitHub using SSH Keys

If you're just starting your developer journey, learning Git and Github is one of the best decisions you can make.
In this article, i'll show you how to set up Git, configure it with your name & email and connect it securely to GitHub using SSH.

What is Git?

Git is a version control tool that runs locally on your computer.
It let's you;
-Track file changes
-Create 'checkpoints' called commits
-Work on branches (separate copy) without affecting the main project

What is GitHub

GitHub is a cloud platform where you can store your Git repositories online, share them with others and collaborate on projects.
It let's you;
-Upload your code(push)
-Download updates(pull)
-Work together with others in real time

1.Setting up Git

How to install and configure Git.

For Windows

Download Git (Install for Windows)
Run the installer and choose "Git default editor" as your terminal
Once installed, open Git Bash and type:

git --version
Enter fullscreen mode Exit fullscreen mode

You should see something like:

git version 2.xx.x
Enter fullscreen mode Exit fullscreen mode

For macOS

Open terminal and run:

git --version
Enter fullscreen mode Exit fullscreen mode

If Git isn't installed, macOS will prompt you to install it via Xcode tools.

For Linux (Ubuntu/Debian)

Run:

sudo apt update
sudo apt install git
Enter fullscreen mode Exit fullscreen mode

2.Configure your Git identity

Before you start using Git, tell it who you are by using your name and email which you used to create your GitHub account.
Run these commands replacing with your details:

git config --global user.name "Your Name"
git config --global user.email "Your Email" 
Enter fullscreen mode Exit fullscreen mode

To confirm your details:

git config --global --list
Enter fullscreen mode Exit fullscreen mode

3.Connecting Git to GitHub Using SSH Keys

SSH(Secure Shell) Keys let you connect to GitHub without typing your password every time.
How to set up:

(i).Generate a New SSH key

ssh-keygen -t ed25519 -C "Your Email"
Enter fullscreen mode Exit fullscreen mode

Press Enter to save it in the default path.

(ii).Generate SSH Agent

eval "$(ssh-agent -s)"
Enter fullscreen mode Exit fullscreen mode

(iii).Add Key to the Agent

ssh-add ~/.ssh/id_ed25519
Enter fullscreen mode Exit fullscreen mode

(iv).Copy your Public Key

cat ~/.ssh.id_ed25519.pub
Enter fullscreen mode Exit fullscreen mode

Copy the whole key that starts with

ssh-ed25519
Enter fullscreen mode Exit fullscreen mode

(v).Add the Key to GitHub

1.Go to GitHub → Settings → SSH and GPG keys
2.Click New SSH key
3.Paste your copied key
4.Save it

(vi).Test the Connection

ssh -T git@github.com
Enter fullscreen mode Exit fullscreen mode

If you see:

Hi yourusername! You've successfully authenticated.
Enter fullscreen mode Exit fullscreen mode

You're all set.

4.Using Version Control with Git

Initialize a Repository

Go to your project folder and intialize Git:

git init
Enter fullscreen mode Exit fullscreen mode

This creates a hidden .gitfolder.

Track changes

Check what's changed:

git status
Enter fullscreen mode Exit fullscreen mode

Add files to be tracked:

git add.
Enter fullscreen mode Exit fullscreen mode

Commit (save) the changes:

git commit -m "Initial commit"
Enter fullscreen mode Exit fullscreen mode

Push code to GitHub

1.Create a new repository on GitHub
2.Copy the repo's SSH url
3.Connect it to your local repo:

git remote add origin git@github.com:username/repo.git
Enter fullscreen mode Exit fullscreen mode

4.Push your code:

git push -u origin main
Enter fullscreen mode Exit fullscreen mode

Now your project is on GitHub.

Pull updates from GitHub

When you make changes on GitHub, pull them locally:

git pull origin main
Enter fullscreen mode Exit fullscreen mode

See your commit history

To view your project's history:

git log
Enter fullscreen mode Exit fullscreen mode

To see a short summary:

git log --oneline
Enter fullscreen mode Exit fullscreen mode

Top comments (0)