Quicksheet · Git
Git Quicksheet: Commands, Undo Recipes and Branching on One Page
Khairul Alam·October 2026·~8 min skim
GitQuicksheetBranching
The commands you reach for most, grouped by task. For the ideas behind them, and how to choose a branching strategy, read the full guide: Version Control, Git and Branching Strategies. Official docs: git-scm.com/docs.
Where each command moves your work.
Setup
| Command | What it does |
|---|
git config --global user.name "Name" | Set the name on your commits |
git config --global user.email "me@x.com" | Set the email on your commits |
git config --global init.defaultBranch main | Name new repositories' first branch main |
git config --global pull.rebase true | Make git pull rebase instead of merge |
git config --list --show-origin | Show every setting and where it comes from |
Start a repository
| Command | What it does |
|---|
git init | Turn the current folder into a repository |
git clone <url> | Copy a remote repository with all its history |
git clone --depth 1 <url> | Shallow clone: latest snapshot only, much faster |
git remote -v | List the remotes |
Save work
| Command | What it does |
|---|
git status -sb | Short status with branch info |
git add <file> | Stage a file |
git add -p | Stage piece by piece (hunks) |
git commit -m "msg" | Commit what is staged |
git commit -am "msg" | Stage tracked files and commit (not new files) |
git commit --amend | Change the last commit (before pushing only) |
git rm <file> / git mv <a> <b> | Delete or rename a file and stage it |
Branches
| Command | What it does |
|---|
git branch -vv | List local branches with their upstream |
git switch -c <name> | Create a branch and move to it |
git switch <name> | Move to an existing branch |
git switch - | Go back to the previous branch |
git branch -d <name> | Delete a merged branch (-D to force) |
git branch -m <new> | Rename the current branch |
git push origin --delete <name> | Delete a remote branch |
Merge and rebase
| Command | What it does |
|---|
git merge <branch> | Merge a branch into the current one |
git merge --no-ff <branch> | Always create a merge commit |
git merge --squash <branch> | Combine the branch into one staged change |
git merge --abort | Cancel a merge that has conflicts |
git rebase main | Replay your commits on top of main |
git rebase -i HEAD~3 | Reorder, squash or reword the last 3 commits |
git rebase --continue / --abort | Carry on or cancel after a conflict |
git cherry-pick <commit> | Copy one commit onto the current branch |
Remotes
| Command | What it does |
|---|
git fetch --prune | Download updates and drop deleted remote branches |
git pull | Fetch and integrate (merge or rebase) |
git push -u origin <branch> | Publish a branch and set its upstream |
git push | Push the current branch |
git push --force-with-lease | Safer force push for your own branch only |
git remote add upstream <url> | Add the original repo when working from a fork |
Look around
| Command | What it does |
|---|
git log --oneline --graph --all | Compact history graph of all branches |
git log -p <file> | History of a file with the changes |
git log -S"text" | Commits that added or removed this text |
git diff / git diff --staged | Unstaged / staged changes |
git diff main...feature | What the feature branch adds since it split |
git show <commit> | One commit in full |
git blame <file> | Who last changed each line |
git shortlog -sn | Commit count per author |
Undo and recover
| Command | What it does |
|---|
git restore <file> | Discard edits in a file (gone for good) |
git restore --staged <file> | Unstage but keep the edits |
git revert <commit> | New commit that undoes an old one. Safe when shared |
git reset --soft HEAD~1 | Undo last commit, keep changes staged |
git reset --mixed HEAD~1 | Undo last commit, keep changes unstaged |
git reset --hard <commit> | Move the branch and throw away changes. Danger |
git clean -fd | Delete untracked files and folders. Use -n first |
git reflog | List where HEAD has been, to find lost commits |
git switch -c rescue <id> | Bring a lost commit back on a new branch |
| Command | What it does |
|---|
git stash push -u -m "wip" | Shelve changes, including untracked files |
git stash list / pop / apply | See, restore and drop / restore and keep |
git tag -a v1.2.0 -m "msg" | Create an annotated release tag |
git push origin --tags | Publish tags |
git worktree add ../fix hotfix/x | Check out another branch in a second folder |
Debugging
| Command | What it does |
|---|
git bisect start / bad / good | Binary-search for the commit that broke something |
git bisect run <test> | Let a script do the bisecting |
git bisect reset | Finish and return to your branch |
git grep "text" | Search tracked files quickly |
.gitignore patterns
| Pattern | Meaning |
*.log | Ignore all files ending in .log |
build/ | Ignore a folder named build anywhere |
/secret.txt | Ignore only in the repository root |
!keep.log | Do not ignore this file, even if a rule above matches |
git rm --cached <file> | Stop tracking a file that is already committed |
Never commit secrets (.env, keys, tokens). If one slips in, treat it as leaked and rotate it.
Which branching strategy?
| Situation | Use |
| Web app or API, deploy often | GitHub Flow, or trunk-based with feature flags |
| Numbered versions, old versions supported | Git Flow, or release/x.y branches |
| Mobile app with store review | Trunk + a release branch per store build, hotfix off the release |
| Open source | Fork and pull request |
| Production fire | hotfix/* from the production tag, then merge back to main |
Rules of thumb
- Short branches, small pull requests, CI on every PR, protected
main.
- Never rebase, reset or force-push commits that others have pulled. Use
revert instead.
- Run
git status before git add . and before every commit.
- Committed work is almost always recoverable with
git reflog. Uncommitted edits are not.
← Back to all writing