Git
beginners
guide
version control
tutorial

Git for beginners The definitive practical guide

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Git becomes much easier once you stop treating it as a bag of commands and start thinking in terms of three places: your working tree, the staging area, and the repository history. A beginner does not need every advanced feature up front; they need a practical workflow they can trust every day.

The Core Mental Model

Git tracks snapshots of files over time. The usual flow is:

  1. edit files in the working tree,
  2. stage the changes you want,
  3. commit that staged snapshot.

Those three states explain most everyday commands.

Start a Repository or Clone One

To create a new repository:

bash
git init

To copy an existing repository:

bash
git clone https://github.com/example/project.git
cd project

Cloning brings down the files, the commit history, and remote-tracking information so you can work locally.

See What Changed

The command beginners should run constantly is:

bash
git status

It answers the most important questions:

  • which branch am I on,
  • what files changed,
  • what is staged,
  • and what is still unstaged.

To inspect content changes:

bash
git diff
git diff --staged

The first shows unstaged changes. The second shows what will go into the next commit.

Stage and Commit

Add files to the staging area:

bash
git add app.py
git add .

Then create a commit:

bash
git commit -m "Add login form validation"

Why stage first? Because it lets you decide exactly what belongs in a commit. Good commits are small, focused, and easy to explain.

Branching Is Cheap and Normal

Branches are lightweight in Git. Use them freely for features or experiments.

bash
git switch -c feature/login-form

That creates and switches to a new branch. When the work is ready, switch back and merge:

bash
git switch main
git merge feature/login-form

This workflow is far safer than editing everything directly on main.

Working With a Remote

Once you have commits locally, send them to a shared remote:

bash
git push origin main

To bring in upstream changes:

bash
git pull

In team environments, many developers prefer to fetch first and inspect:

bash
git fetch
git status
git log --oneline --graph --decorate --all

That makes remote changes more visible before merge or rebase steps.

Fixing Common Mistakes

Staged a file by accident:

bash
git restore --staged notes.txt

Discard an unstaged local change:

bash
git restore notes.txt

Amend the most recent commit message or staged content:

bash
git commit --amend

These commands are worth learning early because they reduce panic when you make an ordinary mistake.

A Safe Everyday Workflow

For many beginners, this sequence covers most of daily Git use:

bash
1git pull
2git switch -c feature/small-change
3# edit files
4git status
5git add .
6git commit -m "Describe the change"
7git push -u origin feature/small-change

Then open a pull request or merge the branch according to your team's process.

Commit Messages Matter

A commit message should explain what changed in a way future readers can understand quickly. Good examples:

  • 'Fix null handling in signup validator'
  • 'Add pagination to orders endpoint'
  • 'Refactor cache invalidation for clarity'

Bad messages such as stuff, update, or fixes save seconds now and cost time later.

Learn a Few Inspection Commands

These commands pay off quickly:

bash
1git log --oneline
2git branch
3git remote -v
4git show HEAD

They help you understand history, branch state, remotes, and the current commit without changing anything.

Common Pitfalls

The biggest pitfall is committing directly to main with unrelated changes mixed together. Small branches and focused commits are safer.

Another mistake is using git add . reflexively without checking git status first. That can stage files you did not intend to commit.

Beginners also often panic at merge conflicts. A conflict is not a repository disaster; it simply means Git needs human help choosing between overlapping edits.

Finally, do not memorize random destructive commands from the internet. Learn the safe inspection and restore commands first so mistakes are recoverable.

Summary

  • Think in terms of working tree, staging area, and commit history.
  • Use git status constantly; it is the most useful everyday command.
  • Make small commits with clear messages.
  • Use branches for features and experiments instead of editing everything on main.
  • Learn safe recovery commands early so ordinary mistakes stay ordinary.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.