The only tutorial you need to setup Git and GitHub

Introduction

Git is a distributed version control system that tracks changes in any set of computer files, usually used for coordinating work among programmers who are collaboratively developing source code during software development. Its goals include speed, data integrity, and support for distributed, non-linear workflows. [Source: Wikipedia]

It is most widely used tool among developers, and often used in pair with GitHub and Gitlab. We are going to learn how to setup Git with GitHub on our system and successfully create a commit and push code to GitHub

Requirements

  1. GitHub account

  2. Git installation

In this tutorial we’ll be carrying out steps on a Windows machine, but the steps are almost same for other MacOS and Linux devices with a slight difference being in the folder structures.

Setting up Git Configurations

Before we can proceed with anything on the GitHub side, we’ll first need to set git configurations on our system.

These are required because every commit in git contains metadata of the person who made the change. These details help in multiple ways :

  • User Identification

  • Collaboration

  • Pushing to remote

Let’s setup the credentials:

  1. git config --global user.name "Your Name"

  2. git config --global user.email "your.email@example.com

Working with GitHub Repositories

While working with GitHub repositories there are multiple ways to work with them.

  • HTTPS method

  • SSH method

  • GitHub CLI

In this tutorial we’ll be focusing only on SSH method, as it is the most secure way and GitHub CLI does use the same internally.

Setting up SSH Keys

  1. Open a terminal of your choice. ( my preference : Windows Powershell )

  2. Run the command: ssh-keygen -t ed25519 -C "your.email@example.com" .

  3. Now, you'll be asked some questions, press enter to set them to default

  4. You'll see public and private keys are generated, meaning you have successfully generated ssh keys.

    ( The key shown on screen is the private key, and make sure to keep it safe)

  5. Go to the location C:/Users/username/.ssh and open the file in text editor id_ed25519.pub.

  6. Open the file id_ed25519.pub in text editor of your choice. And copy the public key.

  7. Next, go to settings section of your GitHub account, and go to SSH and GPG Keys

  8. Click on New SSH Keys and paste the public key.

Congratulations, you have successfully configured SSH keys for your GitHub account, and now you can easily push and pull commits

Why to use the SSH Method ?

Using SSH (Secure Shell) for setting up Git and GitHub has several advantages over the HTTPS method for authentication and communication between your local Git client and GitHub:

1. Increased Security:

  • SSH keys provide a more secure and robust way to authenticate your machine to GitHub without needing to constantly provide a username and password. SSH uses public-key cryptography, where your public key is uploaded to GitHub, and your private key remains on your local machine.

  • With HTTPS, you need to either input your username and password every time or use a Personal Access Token (PAT), which is less secure compared to SSH because it can be more easily compromised if not managed carefully.

2. Password-Free Authentication:

  • Once your SSH key is set up, you don’t need to provide your credentials (username and token) each time you interact with a GitHub repository (pull, push, etc.). The SSH key provides automatic authentication, saving you time and effort.

3. No Need for Personal Access Tokens (PAT):

  • Since August 2021, GitHub no longer allows basic authentication using passwords with HTTPS. Instead, you need to use a Personal Access Token (PAT). PATs are an extra step to manage, whereas SSH simplifies this by using public/private key pairs for authentication.

4. Convenience for Multiple Repositories:

  • With SSH, once the key is set up on your machine and added to GitHub, you can use it for all repositories across your account or organization. You don’t have to configure authentication for each repository individually.

5. Better Support for Automation:

  • If you are automating tasks, such as running CI/CD pipelines or working with GitHub actions, SSH is often more suitable as it can streamline authentication processes without requiring manual intervention for tokens or passwords.

Few Resources

NOTE: These are my personal recommendations, and not any paid endorsement.