Skip to main content

Command Palette

Search for a command to run...

Git for Beginners: Basics and Essential Commands

Basic and essential commands required for using git.

Updated
4 min read
Git for Beginners: Basics and Essential Commands

1. What is Git?

In simple terms, Git is a tool that tracks changes in your files. Think of it as a "time machine" for your code or text projects.

Technically, it is a Distributed Version Control System (DVCS).

  • Version Control: It remembers every change you make. If you make a mistake, you can revert to a previous version (like loading a saved game).

  • Distributed: Every developer working on a project has a full copy of the project history on their own computer, not just on a central server.

Why is Git Used?

  • Collaboration: Multiple people can work on the same file without overwriting each other's work.

  • History & Backup: You can see who changed what, when, and why.

  • Experimentation: You can try out new ideas in a safe, isolated environment (branch) without breaking the main project.

2. Git Basics and Core Terminologies

Before using commands, it is crucial to understand the mental model of Git.

Key Concepts

  • Repository (Repo): The folder containing your project and the hidden .git folder where Git stores all history.

  • Commit: A snapshot of your project at a specific point in time. Think of this as "saving your progress." Each commit has a unique ID (hash).

  • Stage (Staging Area): A preparation zone. You choose which files you want to include in your next commit here.

  • Branch: A parallel version of your project. The main version is usually called main or master. You create new branches to work on features independently.

  • HEAD: A pointer that indicates where you currently are in the project history (usually the latest commit on your current branch).

The Three States of a File

  1. Working Directory: The actual files you see and edit on your computer.

  2. Staging Area: The list of files ready to be committed.

  3. Repository (Directory): The permanent history of your saved snapshots.

The diagram below shows the physical structure of a local repository on your computer, highlighting the hidden .git folder that stores all this data.

3. Common Git Commands

Here are the essential commands you will use 90% of the time.

CommandDescription
git initTurns the current folder into a Git repository.
git statusTells you what files have changed and what is staged.
git add <file>Moves a file from the Working Directory to the Staging Area.
git commit -m "msg"Saves the Staged files as a new snapshot in the Repository.
git logShows a list of all previous commits (history).

4. A Basic Developer Workflow (Step-by-Step)

Let's walk through a real-world scenario from scratch. Imagine you are starting a website project.

Step 1: Initialize the Project

Open your terminal (command prompt), navigate to your project folder, and run:

Bash

git init

Result: Git says, "Initialized empty Git repository." You can now start tracking files.

Step 2: Create a File and Check Status

Create a file named index.html with some text. Now, ask Git what is happening:

Bash

git status

Result: Git will show index.html in red under "Untracked files". It knows the file exists but isn't watching it yet.

Step 3: Stage the File

Tell Git you want to include this file in your next save.

Bash

git add index.html

Result: If you run git status again, the file will appear in green. It is now "Staged" and ready to be committed.

Step 4: Commit (Save) the Changes

Permanently save this snapshot with a descriptive message.

Bash

git commit -m "Create homepage"

Result: Git saves the file. This creates a "save point" you can return to later.

The diagram below visualizes how your commits build up over time, creating a history. It also shows how different branches can diverge and merge.

Step 5: View History

See your progress.

Bash

git log

Result: You will see your Author name, the Date, and your message "Create homepage".