Skip to main content

Command Palette

Search for a command to run...

Git for Beginners: Basics and Essential Commands

Updated
3 min read

What is Git?

Git is a version control system.

In simple words, Git helps us track changes in our code. It allows multiple people to work together on the same project and remembers the full history of changes made to the code.

Why is Git Used?

Earlier and even today some organizations do not use Git to manage their code. In such cases, if someone changes a file, there is no proper way to know:

  • Who made the change

  • What exactly was changed

  • When it was changed

This is where Git comes into the picture.

Git records who made changes, what changes were made, and when they were made. It also allows us to go back to older versions of the code if something breaks.

You might think:

“We can manually save old versions of files in the file system.”

Yes, but:

  • How many versions will you store?

  • How will you name each file?

  • It will consume more storage

  • It becomes very hard to manage

Also, a normal file system does not support collaboration. Git solves all these problems and makes teamwork easy.


Git as a Distributed Version Control System

Git is called a distributed version control system.

Distributed means:

Every developer has a complete copy of the project (including full history) on their own system.

So even if the central server is down, developers can still work.


Git Basics and Core Terminologies

1. Repository (Repo)

A repository is a folder that contains your project and Git history.


2. Commit

A commit is a saved version of your project.

Think of a commit as taking a snapshot (photo) of your code at a specific time.


3. Branch

A branch is a separate path to work on new features or experiments.

Example:

  • Main branch → stable working project

  • Feature branch → new feature or experiment

If the feature works well, it is merged into main.
If not, the branch can be deleted.


4. HEAD

HEAD points to where you are right now in the project.

In simple words:

HEAD points to the latest commit of the current branch.

Example:
Think of a book:

  • The page you are currently reading = HEAD

Common Git Commands (With Simple Examples)

git init

Creates a new Git repository.

git init

Note: Use this once per project.


git status

Shows the current state of your project.

git status

It tells you:

  • Modified files

  • Staged files

  • Untracked files


git add

Moves changes to the staging area.

Add a single file:

git add index.html

Add all files:

git add .

Note: The dot (.) means all files.


git commit

Saves staged changes permanently.

git commit -m "Add homepage layout"

Note: Always write clear commit messages in present tense.


git log

Shows commit history.

git log

Displays:

  • Commit ID

  • Author

  • Date

  • Message

Short and clean history:

git log --oneline

git branch

Shows all branches.

git branch

Create a new branch:

git branch feature-login

git checkout

Switch to another branch:

git checkout feature-login

Create and switch in one command:

git checkout -b feature-login

More Knowledge About Git

  • Git internally uses a Directed Acyclic Graph (DAG).

  • Commits are connected in a graph structure.


git diff

Shows the changes made in files but not yet committed.

git diff

git rebase main

  • Keep commit history clean

  • Avoids duplicate commit messages

  • Preserves commit messages

  • Commit IDs change because history is rewritten.


Squash Merge

  • Combines multiple commits into one

  • May create duplicate commit messages

References: Git Cheatsheet