Skip to main content

Command Palette

Search for a command to run...

Git for Beginners: Basics and Essential Commands

Updated
11 min read
A

learning to survive

Git Git Git…. yeah i know your pain point i have been in your shoes some while before specially if you’re a programmer or developer everyone will keep on exaggerating it to make it sound difficult

i am here to rescue you guys trust me i have read multiple articles around 10-15 after that i m going to write it in a very simple way

we will learn what is Git why you should use it etc.. and after this i know you won’t need any other article let’s get started

remember that read the full article because somethings that you won’t understand at first might be explained later

What is Git?

Git is a distributed version control system that helps developers track changes in code, collaborate safely, and manage different versions of a project

you might be wondering distributed? version control system?

version control system?

Version control is the practice of tracking and managing changes to files, especially source code, over time whatever you change in a file it saves it and keeps the old file with it also

how it works? Every time you edit files, the system saves a snapshot You can compare versions, merge changes, or restore older versions

with vcs you can:-

  • See all the changes made to your project, when the changes were made, and who made them.

  • Include a message with each change to explain the reasoning behind it.

  • Retrieve past versions of the entire project or of individual files.

version control are mainly two types :

  1. Centralized Version Control System

  2. Distributed Version Control System

Centralized Control System:-

in this ,there is a Central Server that stores the code and developers checkout code from this central location to work on it

example:- One Pen Drive in the Class

imagine there is a pen drive in a class which has the project (think what will happen)

everyone must take the pen drive (check out from central location)

edit the files (work on the file)

put it back (to central location)

here can you relate it with the centralized control system?

what happens if one student looses the file ?(think)

here only one person can work at a time because we have only one pen drive

those which don’t have pen drive can’t work

Distributed Control System:

here every developer has a full copy of the project and also it’s history

developers can work independently on their local copies and then share changes with others

it allows developers to work offline and experiment whatever you want to without affection central codebase

Example: Everyone has full copy of the project

suppose in a class teacher gives everyone full copy of the project

each student work in their own way even if one looses the project file it doesn’t make any impact on the project and they can work independently and multiple people can work on the same project at same time

Why Git is Used?

till now you might have got some idea about git know let’s see why it is used

  1. version tracking:

git serves as a system for maintaining the history of the project

  • historical record: used to track changes in files over time

  • Reversibility: it allows developers to go back to previous version of a project if error occur

  • Snapshots: through commits git captures snapshots of your project at specific time

  1. Collaboration:

many users can work at the same time on a same project

and enables them to contribute to same files at same time without overwriting each other’s progress

Git Basics and Core Terminologies

to understand git you need to understand its four building block think of them as ‘infrastructure’ of your project’s history

Repository:

a repository is a folder that Git watches and manages

Why do we need a Repository?

Before Git:

  • You had a normal folder and if we make changes to file the old one is gone and we don’t know what was in it unless we have kept a copy of that same file in some other file

With Git:

  • Git needs one special folder where it can:

    • Track changes

    • Save history (snapshots)

    • Manage versions safely

That special folder is called a repository

Example: project with cctv

  • Your project folder = room

  • Git = CCTV system

  • Repository = room with CCTV installed

If CCTV is not installed:

  • if the cctv is not installed we don’t know what happens in the shop what someone did or what he changed

If CCTV is installed:

we can see what happens because everything is recorded and we can go back rewind if we want to

Repo is folder with Git tracking enabled

What is inside a Repository?

A Git repository contains:

1. Your project files

  • HTML, CSS, JS, Python, etc.

2. Git history (snapshots)

  • All commits

  • All changes

  • Who changed what

3. Branches

  • Main branch

  • Feature branches

4. Hidden .git folder

  • Brain of Git

  • Stores everything (history, snapshots, metadata)

If .git folder is deleted repo is gone

Types of Repositories

  1. Local Repository

  • Lives on your computer

  • Created using git init( will explain it later)

  • You work here daily

Example:

  1. Remote Repository

    stored on the server or the internet and it can be shared with the team

Examples:

  • GitHub

  • GitLab

What is a Commit?

a commit is a saved snapshot of your project

Why do we need Commit?

Without commit:

when you change a code line or update something your old code is gone and you don’t know what you changed and where you changed it

With commit:

with commit every important change is saved and if the code brakes or due to some other reasons you want to go back you can go and you know why change was made

Example: Writing an assignment

you finish chapter 1

you save it as chapter 1 completed

Later:

  • You finish Chapter 2 You save again Added Chapter 2

Each save with a message = commit

What exactly happens when you commit?

When you run:

git commit -m “message”

Git:
1. Takes a snapshot of all staged files
2. Saves it permanently
3. Attaches:

  • message: what was changed or what was updated

  • Author: who changed it

  • Date & time: when

  • Unique ID (hash) its a q=unique value given to every commit

This saved snapshot is called a commit

commit isn’t auto save

Branch

A branch is a separate line of work inside the same project

It lets you work without touching the main code.

Why do we need Branch?

Imagine you have working code now you want to add a new feature or fix a bug or maybe try something different if you change the main code directly if the code breaks whole project breaks Branch solves this problem

Example: Rough notebook

we all used to have two notebooks in school time one was rough one was was the subject notebook now suppose you get a maths problem you don’t solve it directly in main notebook you solve it in rough first then if you get desired result you write it in main notebook or else erase or just cut the rough work

what happens in it is main notebook stays safe there is no confusion

here main notebook is main branch and rough is new branch

Branch in Git

  • Git has a default branch:

    • main (earlier called master)
  • When you create a branch:

    • Git copies the current state ,Both branches now move independently

HEAD

HEAD is a pointer that tells Git: “You are here.”

it shows Which branch you are on, Which commit is currently active

Example: Bookmark in a book

  • Book = Git history

  • Pages = commits

  • Bookmark = HEAD

Wherever the bookmark is:
that’s the page you’re reading now

HEAD in normal use

Usually:

HEAD → main → latest commit

Meaning:

  • You are on main branch

  • At its latest commit

What happens when you make a commit?

HEAD moves forward Points to the new commit

Example:

Before: HEAD → C2

After making a commit : HEAD → C3

HEAD when switching branches

When you do:

git switch feature

HEAD moves:

HEAD → feature → latest commit of feature

Your working files now match that branch.

Detached HEAD

What is Detached HEAD?

HEAD points directly to a commit, not a branch.

Example:

HEAD → C2

This happens when:

git checkout <commit-hash>

Why is it risky?

  • New commits have no branch name, Easy to lose them

Solution:

  • Create a branch if you want to keep the work

Why HEAD is important

  • Tells Git current position

  • Controls:

    • commits

    • merges

    • checkouts

  • Helps Git know what to update

Let’s see Basic Git Commands

first download git from the given site git-scm.com

now we will discuss some git commands and see how they work and we will add files to our Git hub don’t worry everything will be clear at the end

now let’s see step by step what happens

the first thing that we do is create a folder on our system i hope you know how to create a folder

open the git folder shown in the above picture and create a file let’s say we made ash.txt right click on it and it will show such interface

click on open Git Bash a terminal will appear there

when you are in the terminal write the command git init you’ll get a message like this

Initialized empty Git repository in C:/Users/ashfa/Desktop/git/.git/ and a hidden file will be formed .git which will be used to track your folder and it’s not just a normal .git file the connection between you and git depends on it which i will explain in next blog

Check status

git status

it checks which files are modified or which files are newly created like if you don’t remember what you have created or whether you have modified or did something else in a folder you can check by running this command

here see the diagram and take a look

Staging area

when we run the command git add . all the files here ash.txt everything is moved to staging area, staging area is like a waiting area

git add <filename> if we want to add one file at a time

git add <filename> <filename> two at a time etc..

git add is a command used to tell git which changes you want to include in your next commit, when you change or edit a file Git can see those changes but it doesn’t automatically prepare them to be saved

git add is like a selecting photos before uploading them your phone is git it notices bad photos good ones but only the selected photos will be uploaded whether they are bad or good and adds them to staging area where they are ready to be uploaded , let’s say you saw a pic that’s looking ugly you went and changed or added some filter to it you have to again upload that pic manually (hope it clears some doubt)

what happens if you added a file let’s say ash.txt but you saw something is missing and want to remove from staging area you can run the command git reset ash.txt

commit

a commit is like saving your work with a message let’s say you added something to a word into your ash.txt “introduction”

you will write a commit like this git commit -m “added introduction” ,commit should be helpful to like it should provide some idea what you did in it

basically it’s you save your project’s current state and write a message what you changed

now run the command git log it will provide you the information who made commit at what date and on which file the latest commits will appear first then the older

what we done till now we have added our code to local repo but we want to add it into the remote we have to setup the git hub account

let’s go into more detail and add it into the git account

when you login on git you will have an option to create a repository click on create repository after that you will get certain commands

git remote add origin https://github.com/username/repo-name.git paste them into terminal and then wait for sometime to see your repo on the git what it does is simply links your project to Git hub

git push -u origin main run this command it sends your commits to Git hub

-u sets default push branch and main is the branch name after all this it will ask for username name and password login git and your project is pushed to remote repo, the above picture your terminal should look like this if it’s not looking like this see where you’re making an error

let’s see the commands one last time

Essential Commands List: these are little advanced one

  • git branch: List/create branches.

  • git switch or git checkout: Switch branches.

  • git merge: Combine branches.

  • git pull/git push: Sync with remote.

  • git clone: Copy a remote repo.

  • git remote add origin: Link local to remote.

  • Error handling: git reset, git revert.

at first we run the command git init →git add <filename> → git status→ git commit -m “write the change you did” →local repo —-→push→ remote repo

i hope you have got little bit understanding of how git works and what git actually is why we should use it

sorry for such long blog and thanks if you’re still reading