What is Git?

Git is a version control system. A version control system allows you to record changes made to a file or a folder over time. This lets us see the timeline of a project and literally revert to an older version of the project.

What is GitHub?

GitHub is a platform that takes Git to a next level. Any project that utilises the power of Git can make their repository (Git initialised project) remote (online), allowing developers from around the world to see your code and make their own contributions.

Prerequisites

Installing Git

First, you need to install Git in your computer. For windows, you can download Git for Windows.

The following steps are applicable to Linux users:

  1. Install Git using APT:
    • sudo add-apt-repository ppa:git-core/ppa
    • sudo apt update
    • sudo apt install git
  2. Verify the Git you installed has version atleast 2.28:
    • git --version

Configure Git and GitHub

Setup Git

  1. Add your name and email address:
    • git config --global user.name "<Your name>"
    • git config --global user.email "<youremailaddress>"
  2. Change the default branch from master to main:
    • git config --global init.defaultBranch main
  3. Enable colourful output for Git:
    • git config --global color.ui auto
  4. Verify the details you entered:
    • git config --get user.name
    • git config --get user.email

Create a GitHub account

Go to GitHub.com and create an account for free.

Create an SSH Key

SSH key is required to connect Git and GitHub.

Link Your SSH Key with GitHub

The next step is to Link Your SSH Key with GitHub. Log in to your GitHub account, click on your profile picture in the top right corner. Then, click on Settings in the drop-down menu.

Next, on the left-hand side, click SSH and GPG keys. Then, click the green button in the top right corner that says New SSH Key. Name your key something that is descriptive enough for you to remember where it came from. Leave this window open while you do the next steps.

Now you need to copy your public SSH key. To do this, execute this command:

cat ~/.ssh/id_rsa.pub

Now, go back to GitHub in your browser window and paste the key you copied into the key field. Then, click Add SSH key.

Testing your key

Use this command and type yes:

ssh -T git@github.com

If you did everything correctly, you will see this message:

Hi <username>! You’ve successfully authenticated, but GitHub does not provide shell access.

Creating Your First Repository

A repository is a project that has Git initialised. You can initialise Git in any project using this command:

git init

However, this repository is only visible to you. To make your repository visible to the internet and to collaborate with other developers, you need a remote repository. You can use GitHub for this.

Get started by logging into your GitHub account and clicking on the + icon at the top and then clicking “New Repository”. Give it any name you like.

Cloning Your Repository

To work on your repository, you have to clone it into your local machine. Use this command:

git clone <SSH URL>

This will clone the repository into the current working directory. Now you can start working on your project.

Use the Git Workflow

  1. Cd into the cloned repository and create a file you like:
    • touch <filename>
  2. Now let’s check the status of the repository:
    • git status
  3. We can see that the file that was just created is not staged. To stage it, use this command:
    • git add <filename>
  4. Now if we check git status again, we can see that the file has been added to the staging area. Now, to commit the changes, type:
    • git commit -m "<Commit message>"

You can use this workflow to build your project.

Publishing Your Changes to GitHub

Even though you staged and commited the changes in your local repository, your remote repository will not become upto date, unless you push the changes to GitHub.

Use this command to push the changes:

git push origin <branch-name>

References