Git Quick Reference¶
Branching¶
| Command | What it does |
|---|---|
git switch -c <branch> |
Create and switch to a new branch |
git switch <branch> |
Switch to an existing branch |
git branch -d <branch> |
Delete a local branch (safe — refuses if unmerged) |
git push origin <ref>:refs/heads/<new-branch> |
Create a remote branch from any ref without creating a local branch |
Inspecting¶
| Command | What it does |
|---|---|
git log --oneline |
Compact commit history |
git log --oneline <base>..<branch> |
Commits on <branch> not on <base> |
git diff <branch> |
Show what's changed compared to a branch |
git diff --staged |
Show what's staged for the next commit |
git stash list |
List all stashes |
Staging & Committing¶
| Command | What it does |
|---|---|
git add -p |
Interactively stage hunks |
git commit --amend |
Rewrite the last commit (message or contents) |
git stash |
Stash uncommitted changes |
git stash pop |
Restore the most recent stash |
Rewriting History¶
| Command | What it does |
|---|---|
git reset --soft HEAD~1 |
Undo last commit, keep changes staged |
git reset --hard <target> |
Reset branch to match <target> exactly (destructive) |
git rebase <base> |
Replay current branch on top of <base> |
git cherry-pick <commit> |
Apply a single commit onto current branch |
Pushing & Syncing¶
| Command | What it does |
|---|---|
git fetch --prune |
Fetch and remove stale remote-tracking branches |
git pull --rebase |
Pull and replay local commits on top |
git push -u origin <branch> |
Push and set upstream tracking |
git push --force-with-lease origin <branch> |
Force push, but abort if remote has changed since last fetch |