Gitastic

“I want to…”

Situations mapped to commands, grouped by what you want to do. Each links to the lesson that explains it.

Everyday

See what changed and what is staged
git status
Stage a file for the next commit
git add <file>
git add . stages everything→ The three areas
See unstaged / staged changes
git diff · git diff --staged
Commit what is staged
git commit -m "message"
Stage all tracked changes and commit
git commit -am "message"
new (untracked) files still need git add→ The three areas
Show compact history of everything
git log --oneline --graph --all
Inspect one commit
git show <commit>

Branches

Start a new branch and switch to it
git switch -c <name>
Switch to an existing branch
git switch <name>
List branches (incl. remote)
git branch -a
Bring another branch into mine
git merge <branch>
Replay my branch on top of main
git rebase main
only for commits nobody else has→ Rebase: replaying commits
Give up on a conflicted merge
git merge --abort
Finish a conflict
edit files → git add <file> → git commit
rebase: git rebase --continue→ Merge conflicts, calmly
Delete a merged branch
git branch -d <name>

Team & remotes

Download news without changing my work
git fetch
Update my branch from the remote
git pull
git pull --rebase for a straight line→ Pull & push
Publish a new branch
git push -u origin <branch>
My push was rejected
git pull → git push
someone pushed first; integrate their work→ Pull & push
Overwrite my own remote branch after a rebase
git push --force-with-lease
Destructive→ Pull & push
Clean up after my PR was merged
git switch main && git pull && git branch -d <branch>

Undo & rescue

Throw away changes to a file
git restore <file>
Destructiveuncommitted changes are gone for good→ The undo toolbox
Unstage a file (keep the changes)
git restore --staged <file>
Fix the last commit (message or content)
git commit --amend -m "better"
only if not pushed yet→ The undo toolbox
Undo last commit, keep changes staged
git reset --soft HEAD~1
Undo last commit, keep changes unstaged
git reset HEAD~1
Undo last commit and destroy the changes
git reset --hard HEAD~1
Undo a commit that is already pushed
git revert <commit>
adds a new "opposite" commit — safe for shared history→ The undo toolbox
Park unfinished work
git stash → git stash pop
Find "lost" commits
git reflog
Get a lost commit back
git branch rescue <hash>
or git reset --hard HEAD@{1}→ Detached HEAD & the reflog safety net
I am in "detached HEAD" and want to keep my work
git switch -c <new-branch>

Power tools

Copy one commit onto my branch
git cherry-pick <commit>
Work on another branch in a second folder
git worktree add ../hotfix -b hotfix
List / remove worktrees
git worktree list · git worktree remove ../hotfix
Mark a release
git tag v1.0 && git push origin v1.0
Squash my last 3 local commits
git reset --soft HEAD~3 && git commit -m "…"
or interactively: git rebase -i HEAD~3→ Tags & tidy history