Git and the command line: 50 essentials
The Git commands and shell skills developers use every day: branching, rebasing, undoing mistakes, pipes, redirects, permissions and processes.
Practice now
Study without an account
Scheduled with the default Askesis timings. Progress is saved in this browser.
All 50 cards
- 1Start a new repository in the current folderbash
git init - 2Stage changes hunk by hunkbash
git add -p - 3Fix the last commit’s message or contentsbash
git commit --amendThis rewrites the commit; avoid it on commits you’ve already pushed and shared. - 4Compact history graphbash
git log --oneline --graph --all - 5
git diffvsgit diff --stagedgit diffshows unstaged changes;--stagedshows what will go into the next commit - 6Create a branch and switch to itbash
git switch -c feature/login - 7What does
git mergedo?Combines another branch into the current one, with a merge commit unless it can fast-forward - 8What does
git rebase maindo?Replays your branch’s commits on top ofmain, giving a linear history - 9Merge vs rebaseMerge keeps history as it happened; rebase rewrites it to be linear. Don’t rebase commits others already have
- 10Fast-forward mergeWhen the target hasn’t diverged, Git just moves the branch pointer forward; no merge commit
- 11Set work aside and get it backbash
git stash git stash pop - 12
git reset --soft HEAD~1Undo the last commit, keeping its changes staged - 13
git reset HEAD~1(mixed, the default)Undo the last commit, keeping its changes in the working tree, unstaged - 14
git reset --hard HEAD~1Undo the last commit and discard its changes entirelyDestructive.git reflogcan still find the commit for a while. - 15Undo a pushed commit safelybash
git revert <commit>Creates a new commit that reverses it, without rewriting history. - 16Find a commit you “lost” after a reset or rebasebash
git reflogThe reflog records every position HEAD has been at. - 17Apply one commit from another branchbash
git cherry-pick <commit> - 18
git fetchvsgit pullfetchdownloads remote changes;pullfetches and then merges (or rebases) them - 19Push a new branch and track itbash
git push -u origin feature/login - 20Safer force pushbash
git push --force-with-leaseRefuses if the remote moved since your last fetch, so you don’t overwrite others’ work. - 21What is
.gitignorefor?Patterns of untracked files Git should ignore, like build output and secrets - 22Discard local changes to a filebash
git restore notes.md - 23Unstage a file but keep its changesbash
git restore --staged notes.md - 24Detached HEADHEAD points directly at a commit instead of a branch; new commits are easy to lose unless you create a branch
- 25Find the commit that introduced a bugbash
git bisect start git bisect bad git bisect good v1.0Git binary-searches history while you mark commits good or bad. - 26Create an annotated tagbash
git tag -a v1.0 -m "First release" - 27Who last changed each line of a file?bash
git blame app.ts - 28List remotes with their URLsbash
git remote -v - 29
HEAD~1vsHEAD^2HEAD~1is the first parent;HEAD^2is the second parent of a merge commit - 30Show files changed in the last commitbash
git show --stat - 31Print the current directorybash
pwd - 32List all files, including hidden ones, with detailsbash
ls -la - 33Go back to the previous directorybash
cd - - 34Search files recursively, with line numbersbash
grep -rn "TODO" src/ - 35Find .log files modified more than 7 days agobash
find . -name "*.log" -mtime +7 - 36What does a pipe
|do?Sends one command’s standard output into the next command’s standard input - 37
>vs>>>overwrites the file;>>appends to it - 38Send errors to the same place as outputbash
command > out.log 2>&1 - 39Make a script executablebash
chmod +x deploy.sh - 40What does
chmod 755mean?Owner can read, write and execute; group and others can read and execute - 41What does
chmod 644mean?Owner can read and write; group and others can only read - 42Find running node processesbash
ps aux | grep node - 43
killvskill -9killsends SIGTERM (asks the process to exit);kill -9sends SIGKILL, which can’t be caught - 44Delete files found by
findbashfind . -name "*.tmp" -print0 | xargs -0 rm-print0/-0handle file names with spaces. - 45Show the exit status of the last commandbash
echo $?0 means success. - 46
&&vs||between commandsa && bruns b only if a succeeded;a || bruns b only if a failed - 47Follow a log file as it growsbash
tail -f app.log - 48Count lines in a filebash
wc -l data.csv - 49Count how often each line occursbash
sort access.log | uniq -c | sort -rnuniqonly merges adjacent lines, so sort first. - 50Generate an SSH keybash
ssh-keygen -t ed25519 -C "[email protected]"
Frequently asked questions
Which is safer, reset or revert?
For commits that are already pushed, revert: it adds a new commit instead of rewriting shared history.
Do the shell commands work on macOS?
Yes. They work in bash and zsh on Linux and macOS, and in WSL or Git Bash on Windows.
Can I recover a commit after git reset --hard?
Usually, yes: find it with git reflog and reset or branch to it, before Git garbage-collects it.
More in Programming
JavaScript
Master the JavaScript every developer uses daily: scope, closures, equality, array methods, promises, async/await and the event loop.
50 cardsAdvanced JS
Fifty advanced JavaScript cards: event-loop ordering puzzles, generators and async iteration, this binding, Proxy and Reflect, modules, AbortController and coercion edge cases.
50 cardsTypeScript
Learn the TypeScript you use every day: narrowing, generics, keyof, mapped and conditional types, utility types, satisfies and strict mode, with code on every card.
50 cardsPython
Learn idiomatic Python fast: comprehensions, f-strings, args and kwargs, generators, decorators, context managers and the standard library.
50 cardsJava
Core Java with short snippets: equals vs ==, records, sealed types, collections, generics, exceptions, streams, lambdas and JVM basics.
50 cardsC#
C# and .NET fundamentals with short snippets: value vs reference types, properties, records, nullable reference types, LINQ, async/await and DI.
50 cardsKotlin
Kotlin fundamentals with short snippets: null safety, val vs var, data and sealed classes, extension functions, scope functions and coroutines.
50 cardsSwift
Swift fundamentals with short snippets: optionals, structs vs classes, protocols, enums with associated values, closures, ARC, async/await and SwiftUI.
50 cardsFlutter
Flutter and Dart basics: null safety, futures and streams, stateless vs stateful widgets, setState, hot reload, keys, layout and state management.
50 cardsPHP
Modern PHP 8 with short snippets: strict types, arrays, match, nullsafe and named arguments, enums, readonly, Composer, PDO and password_hash.
50 cardsRuby
Ruby fundamentals with short snippets: blocks, procs and lambdas, symbols, Enumerable, mixins, truthiness, method_missing, gems and Bundler.
50 cardsGo
Go (Golang) fundamentals with short, idiomatic snippets: slices vs arrays, maps, defer, error wrapping, interfaces, goroutines, channels, select and context.
50 cardsRust
Learn the Rust that the compiler keeps asking about: ownership, borrowing, lifetimes, Option and Result, traits, closures, smart pointers and async.
50 cardsC++
Modern C++ in short snippets: references vs pointers, RAII, smart pointers, move semantics, STL containers and algorithms, const, templates and UB.
50 cardsHaskell
Haskell fundamentals with short code: types and type classes, pattern matching, laziness, Maybe and Either, folds, Functor, Applicative and Monad.
50 cardsElixir
Elixir fundamentals with short snippets: immutability, pattern matching, the pipe operator, Enum vs Stream, processes, GenServer, supervisors and mix.
50 cardsx86-64 assembly
x86-64 assembly in Intel syntax: registers and sub-registers, flags, mov and lea, jumps, the stack frame, the System V calling convention and Linux syscalls.
50 cardsRegex
Learn regular expressions for good: anchors, classes, greedy vs lazy quantifiers, groups, lookarounds, flags, replacements and the backtracking trap.
50 cards