Skip to main content

Command Palette

Search for a command to run...

Git for Beginners: Basics and Essential Commands

Get well versed with the essential git commands ✨

Updated
•4 min read
Git for Beginners: Basics and Essential Commands
G
I enjoy blending technology with business to build solutions that create real impact. I’m fascinated by how technology empowers people, businesses, and startups. I believe thoughtful use of technology quietly improves lives, and that belief fuels everything I build and explore 💻.

As we have understood in the earlier blog about the need of using a version control system and what problems it solves. In this blog we will be seeing about the Basic and some essential commands of the most popular version control system - which is Git .

NOTE : If you want to first understand the reason behind the need of a Version Control System , you can go though my blog - “Why Version Control Exists: The Pen-drive Problem”. Here is the Link of my previous blog: https://gaurangpods.hashnode.dev/why-version-control-system-exists-the-pen-drive-problem

Once you have an understanding about the need of a VCS , You can easily relate to the basic and essential commands section here.

  1. What is Git : Git is the most popular Version Control System amongst all the VCS. It came into existence in 2005.

  2. Why Git is Used : To solve the “Live Collaboration” and “Out of Sync Code” Problem.
    Live collaboration : Multiple developers can work on the same project without overwriting each other’s work.
    Out-of-sync Code Problem : Git keeps everything in sync using commits + branches, so nobody has to keep sending files over email / pen-drive.

  3. Git Basics and Core Terminologies :

    i.) Working Directory : This is your normal project folder where you edit code.
    You write code, You edit README, You add a new file etc… All of this is happening in the working directory.

    ii.) In day to day language what we call as “Folder”, when that folder starts containing a “.git/” folder inside it then it is called by the name of “Repository” in the world of git.

On running “git init” command we basically initialises a repository aka repo, Just remember the above Image ,it will make you understand the flow and commands easily.

iii.) Staging Area : “git add . “ moves changes from Working Directory → Staging Area.

In Simple terms to take snapshot of the present code written in your IDE, use “git add <filename>”
or ” git add .” . It takes the snapshot of the code. NOTE : But It does not saves the Snapshot.

iv.) Commit : To save the snapshot , we use “Git commit -m”” ” command. The snapshot is saved in local repo

v.) Push : Then we use : “git push” which basically pushes the repository from local to the remote. So, it saves the snapshot in the remote repo ie on Server.

vi.) Branch : Think of a branch as a separate timeline of code. developers create branches like:

feature/navbar

bugfix/login-issue

refactor/api

They work safely there, then later merge it into main Branch.
So basically, There is one main branch of your git repo, and then to work with live collaboration , we use the concept of branch, developers create separate branches, work upon them and then merge it to the main branch if their code is not buggy,

vii.) HEAD is a file inside the “.git/” folder that contains the reference of the current branch.

4. Git essential commands :

i.) To create a “.git/” folder inside any folder, In the terminal write the command “git init” inside the folder you want to create “.git/” folder .

After running “git init” command in the terminal, wherein I have opened a folder names as “Piyush_AI_Series” :- You can see in the below picture that a “.git/” folder got created . However since it always is a hidden folder, hence we had to run “ls -a” command which shows/ lists all the files in that folder.

ii.) git add <filename> or “git add .” basically stages our code ie takes the snapshot but does not saves it.

iii.) git status helps us in understanding the status or difference(if any in code) between the Working Directory and the staging Area.

iv.) git commit or “git commit -m ““ “ command basically saves the snapshot of the the code into our “.git/” folder which basically is also called as Local Repository.

v.) git push command basically pushes the saved snapshot from the local repository to the remote repository. Remote repository is the repository that is hosted on the servers , which in our case is Github.

vi.) git log is the command that gives us the information about all the commits that have been done by us.