Git
Beginner 12 min read Git

Git Basics & Workflow

Git is a distributed version control system built around three states: working directory, staging area and repository. Master init, add, commit, log and status to understand every other command.

version controlcommitstaging arearepositorygit initgit log
Introduction

What is this?

Git tracks changes to files as a series of snapshots. Every commit records a full snapshot plus metadata (author, message, timestamp) and gets a unique SHA-1 hash. Git is distributed: every clone contains the entire history, so it works fully offline.

Motivation

Why do you need it?

  • Every commit is a complete, hash-addressed snapshot, so any state in history can be reproduced exactly.
  • It is distributed — every developer has a full copy of history and can commit without a network connection.
  • Changes are atomic and reviewable: each commit is a self-contained unit of work with an author and reason.
  • git blame and git log turn history into an audit trail for who changed what and when.
Think about it

Mental model

Imagine

A lab notebook

Think of

Git history

Every dated entry records what changed and why, and you can flip back to any page. Git pages are signed with a hash so nobody can quietly rewrite the record.

Mechanics

How it works

  • git init creates a hidden .git directory that stores all history and configuration for the repository.
  • Files live in the working directory; git add moves changes from there into the staging area (the index).
  • git commit records the staged snapshot, generates a SHA-1 hash, and stores it in the repository.
  • git status shows which files are modified, staged or untracked; git log shows the committed history.
  • git diff compares the working tree, index and commits so you can review changes before committing.

Git's three-tree model: working directory, staging area, repository.

Working directory
git add
Staging area (index)
git commit
Repository (.git)
Reference

Key commands

git init

Create a new empty repository in the current directory.

git status

Show which files are modified, staged or untracked.

On branch main\nChanges to be committed: ...\nUntracked files: ...
git add <file>

Stage a file or directory so it is included in the next commit.

git commit -m 'message'

Record the staged snapshot with a message.

git log --oneline

Show commit history, one line per commit.

a1b2c3d (HEAD -> main) Add project README
git diff

Show unstaged changes between the working directory and the index.

Hands-on

Examples

Your first commit

Your first commit
git init
echo "# My Project" > README.md
git add README.md
git commit -m "Add project README"
git log --oneline

git init creates the repository, README.md is created and staged with git add, then git commit records the snapshot. git log --oneline shows one line per commit so you can see the hash and message.

Real world

In production

  • CI/CD pipelines run a build and tests on every commit pushed to the repository.
  • Pull requests group commits into reviewable units, and git blame traces regressions to the commit that introduced them.
  • Release tags and commit SHAs make every deployed artifact traceable back to source.
  • Semantic commit conventions feed automated changelog and version-bumping tools.
Guidelines

Best practices

  • Commit small, focused units of work with a clear message; one commit = one logical change.
  • Review your staged diff with git diff --staged before committing.
  • Use a .gitignore to keep build artifacts, local configs and secrets out of the repository.
  • Configure user.name and user.email before your first commit.
  • Prefer Conventional Commits (fix:, feat:, chore:) so tooling and release notes can parse history.
Watch out

Common mistakes

  • Running git add . and committing secrets, logs or build artifacts by accident.
  • Making huge 'WIP' commits that are impossible to review or bisect.
  • Forgetting to configure user.email — git refuses to commit without it.
  • Tracking generated files, which produces noisy diffs and merge conflicts.
Root cause analysis

Common issues

Error
Please tell me who you are. fatal: unable to auto-detect email address
Root cause

user.name or user.email is not configured.

Fix

Run git config --global user.name 'Your Name' and git config --global user.email 'you@example.com'.

Error
fatal: not a git repository (or any of the parent directories)
Root cause

You ran a git command outside a repository.

Fix

Run git init to create a repository, or cd into the correct repo directory.

Error
nothing to commit, working tree clean
Root cause

Nothing is staged or modified.

Fix

Make or edit a file, stage it with git add, then commit.

When things break

Debugging tips

  • git status is always the first step — it tells you exactly where your changes are.
  • git diff shows unstaged changes; git diff --staged shows what the next commit will contain.
  • git log -p shows each commit's patch so you can see how files evolved.
  • git status --short gives a compact one-character-per-file view for scripting.
Interviews

Interview checkpoint

Practice

Hands-on lab

Your First Repository
20 min · beginner
In progress
Objectives
  • Configure Git for the first time
  • Initialize a repository
  • Stage and commit changes
  • Inspect history with git log and git diff

Your First Repository

20 min · beginner

0/5 steps
0%
sandbox · Linux sandbox
$
Try these commands
Test yourself

Quick quiz

0/3
1

Which command moves changes from the working directory into the staging area?

2

Git requires a network connection to make a commit.

3

Which command shows the commit history in compact one-line form?

Quick reference

Cheat sheet

Daily workflow
git initCreate a repository
git statusCheck working tree state
git add <file>Stage a change
git commit -m 'msg'Commit staged changes
git log --onelineCompact history
git diffShow unstaged changes
Go deeper

Official documentation