Skip to main content
KO
guide

20 Essential Git Commands for Beginners — From Basics to Undoing Mistakes

2026-04-30 · 8 min read

The Three Places Git Cares About

Git tracks files across three areas. Internalizing these makes every command click:

  • Working directory — the files you're editing right now.
  • Staging area (index) — changes you've queued for the next commit.
  • Repository — the committed history stored in the .git folder.

The daily cycle is: edit → git add (to staging) → git commit (to repo).

One-Time Setup

git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main

Starting or Joining a Repo

  • git init — turns the current folder into a Git repo.
  • git clone <url> — downloads an existing remote repo.

The Daily Commands

git status

The command you run more than any other — shows what's changed, staged, and untracked. Run it before and after almost every other command.

git add

Stages changes for the next commit.

git add README.md       # one file
git add src/            # a directory
git add .               # everything in current path

Check git status before git add . — sensitive files (.env, credentials) can sneak in.

git commit -m "..."

Records the staged changes. Good messages explain why, not what (the diff already shows the what). Conventional prefixes like fix:, feat:, docs: help scanning.

git log

Shows commit history.

git log --oneline --graph

git diff

Compares states.

git diff              # working vs staged
git diff --staged     # staged vs last commit
git diff HEAD         # working vs last commit

Working With Remotes

  • git remote -v — lists connected remotes (origin by convention).
  • git push origin main — uploads commits. First push of a new branch uses -u to set tracking: git push -u origin feature/x.
  • git pull — fetches and merges in one step.
  • git fetch — downloads remote changes without merging.

Branches

git branch                   # list local branches
git switch main              # move to main
git switch -c feature/login  # create and move in one step

Merge another branch into yours:

git switch main
git merge feature/login

Conflicts show <<<<<<<, =======, >>>>>>> markers — edit the file, then git add and git commit.

Delete a merged branch with git branch -d feature/login.

Undoing Mistakes Safely

git restore

git restore file.js          # discard working-dir changes
git restore --staged file.js # unstage but keep changes

git reset

Three flavors, in order of destructiveness:

  • --soft HEAD~1 — undo last commit, keep changes staged
  • --mixed HEAD~1 (default) — undo commit, unstage, keep changes in working dir
  • --hard HEAD~1 — undo commit and delete changes. Use with care and never on published commits.

git revert

The correct way to undo something that's already been pushed. Creates a new commit that reverses the target commit, preserving history.

git revert 3a2b1c4

git stash

Sets aside uncommitted work when you need to switch contexts.

git stash
git stash pop
git stash list

git reflog

A safety net that lists every recent position of HEAD. Even after a --hard reset, the old commit is usually still reachable here for a while.

git reflog
git reset --hard HEAD@{2}

.gitignore

List things you don't want tracked:

node_modules/
dist/
.env
.DS_Store
*.log

If you accidentally committed a file before ignoring it, remove it from the index with git rm --cached <file> and commit.

Three Habits That Prevent Pain

  1. Run git log and git diff before pushing.
  2. Avoid --force push on shared branches; never on main/master.
  3. Keep commits small and focused — one topic per commit makes history readable and revertable.

When You're Stuck

  • git status is a built-in hint system — it often tells you exactly what to do next.
  • git help <command> opens detailed docs.

Master these twenty commands and you'll handle the vast majority of day-to-day Git work. Rebase, cherry-pick, bisect, and submodules can wait until these feel automatic.