Git Cheat Sheet and Exercises
This Git cheat sheet collects the commands from the whole tutorial in one place, followed by exercises that walk you through a realistic workflow. Bookmark it for quick reference.
Setup and Basics
- git config --global user.name "Name" — set your name
- git init — create a repository
- git clone <url> — copy a remote repository
- git status — see the state of your files
- git add <file> / git add . — stage changes
- git commit -m "message" — save a snapshot
- git diff / git diff --staged — see changes
- git log --oneline --graph --all — view history
Branching and Merging
- git branch — list branches
- git switch -c <name> — create and switch
- git switch <name> — change branch
- git merge <branch> — merge into current branch
- git rebase main — replay your branch onto main
- git rebase -i HEAD~3 — edit, squash or reword recent commits
- git branch -d <name> — delete a merged branch
Remotes
- git remote add origin <url> — link a remote
- git push -u origin <branch> — first push of a branch
- git fetch — download without merging
- git pull / git pull --rebase — download and integrate
- git push --force-with-lease — safe force push after rewriting
Undo and Save for Later
- git restore <file> — discard unstaged edits
- git restore --staged <file> — unstage
- git commit --amend — fix the last commit
- git revert <commit> — undo a commit with a new commit
- git reset --soft HEAD~1 — undo last commit, keep changes
- git stash / git stash pop — shelve and restore work
- git reflog — find lost commits
Exercise: A Complete Workflow
Run these commands in an empty folder and predict the output of each git log before running it.
git init practice && cd practice
echo "Hello" > app.txt
git add app.txt && git commit -m "Add app"
git switch -c feature/greeting
echo "Hello, world" > app.txt
git commit -am "Expand greeting"
git switch main
echo "Hi" > app.txt
git commit -am "Shorten greeting"
git merge feature/greeting # conflict! resolve it
git add app.txt && git commit -m "Merge greeting"
git log --oneline --graph* e5f6a7b (HEAD -> main) Merge greeting |\ | * c3d4e5f (feature/greeting) Expand greeting * | d4e5f6a Shorten greeting |/ * b2c3d4e Add app
More Exercises
- Add a .gitignore that ignores *.log, then create debug.log and confirm git status ignores it.
- Make three small commits and squash them into one with interactive rebase.
- Create a commit, undo it with git reset --hard, then recover it with git reflog.
- Push the practice repo to a new GitHub or GitLab repository and tag it v1.0.0.
- Open a pull request from a feature branch and merge it with "Squash and merge".
Frequently asked questions
What are the most used Git commands?
git status, add, commit, push, pull, switch, branch, merge, log and diff cover the vast majority of day-to-day work.
How can I practice Git safely?
Create a throwaway repository in a temporary folder and experiment freely. Interactive sites like Learn Git Branching also visualize commands as you type them.
What should I learn after the Git basics?
Learn your team's pull request workflow, interactive rebase, git bisect for finding bugs, and CI pipelines with GitHub Actions or GitLab CI.