Git basics to get started

Git basics to get started

Git? GitHub? What are they? How to work with Git? Today we will see how to get started with Git and see how they work.

Introduction

Hello everyone, I hope you all are doing great. Today we are going to talk about the very basics of Git and learn some basics Git commands to get you started with Git. So let's get started.

What is Git?

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

But what does this mean? In layman's term, Git is like a diary. Just like how you write what you did the whole day in a diary, Git does it for computer files. Git keeps a record and tracks the changes made in files in a git repository. Just like how you have to sit down and write what happened in your day in a diary, you have to write down what you changed in a file to Git. Git is just a diary. So let's get started with how to write this Git diary.

How to use Git

Creating a Git repository

After installing Git, Git will not automatically record and track all the files. You have to specify which folder the git needs to keep track of. This folder which the Git records and tracks the files is called a Git Repository.

To initiate a Git Repository to a folder, we need to open the Terminal / Command line and type the following.

$ git init
  • git : This is the name of the program we are going to execute.
  • init : This is like a sub command which tells the git to initialize the git repository in the current folder.

Perfect! We initialized the repository, now let's see what we have to do next.

Staging files

What does staging file mean? Let's say you initialized a git repository in your folder named git-learning and now let's say you created 3 files in this folder index.html, style.css and background.png. Now you have to tell Git that you created these 3 files. How git works is, it packages all the changed files into one box to make the user easier to work with multiple files. To package multiple files into a box, we need to type the following command.

$ git add .
  • git : This is the name of the program we are going to execute.
  • add : This is like a sub command which tells the git to add one/more files to a box and package it.
  • . : This dot tells the git to add all files which are inside the folder we are currently in into the package.

Superb! We packaged all the changed files into one box. Now we have to write this into our diary that we did this. Let's do that!

Committing changes

Till now, we only packaged all changed files into one box. Now we have to write this into our so-called diary. This is where committing comes in. Commit is like writing the entry in the diary. To write this, we need to write this following command.

$ git commit -m "My first commit!!"
  • git : This is the name of the program we are going to execute.
  • commit : This is like a sub command which tells git to write the changes into its diary.
  • -m : This is called a commit message, you need to give a string of message after this to tell what you changed in this package.

Kudos!! We just did our first commit! This commit is now etched into Git, now we can come back to this commit even in the future and see what files we changed.

Difference between Git and GitHub

We only learned Git, What about GitHub? What's the difference between these both? Let's see them.

As I said, Git is like a diary, and we can write things into it, and it will keep track and record the changes of the files in a Git repository. GitHub, on the other hand, is a cloud platform where you can store this Git repository. GitHub can store your entire Git repository on the cloud so that you can download this repository to any computer which has an Internet connection.

Let's see this image, I think it will explain how this whole process works a bit more clearly.

git.png

I hope you got a better understanding about Git and how to get started with it, I will see you next week with another article. Bye!

Did you find this article valuable?

Support Bharath Shanmugam by becoming a sponsor. Any amount is appreciated!