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.

Workingdirectory Staging areathe index Localrepository Remoteorigin addcommitpush pull = fetch + merge (or rebase) restore: staging → working  ·  reset: repository → staging  ·  fetch: remote → local repository
Where each command moves your work.

Setup

CommandWhat 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 mainName new repositories' first branch main
git config --global pull.rebase trueMake git pull rebase instead of merge
git config --list --show-originShow every setting and where it comes from

Start a repository

CommandWhat it does
git initTurn 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 -vList the remotes

Save work

CommandWhat it does
git status -sbShort status with branch info
git add <file>Stage a file
git add -pStage 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 --amendChange the last commit (before pushing only)
git rm <file> / git mv <a> <b>Delete or rename a file and stage it

Branches

CommandWhat it does
git branch -vvList 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

CommandWhat 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 --abortCancel a merge that has conflicts
git rebase mainReplay your commits on top of main
git rebase -i HEAD~3Reorder, squash or reword the last 3 commits
git rebase --continue / --abortCarry on or cancel after a conflict
git cherry-pick <commit>Copy one commit onto the current branch

Remotes

CommandWhat it does
git fetch --pruneDownload updates and drop deleted remote branches
git pullFetch and integrate (merge or rebase)
git push -u origin <branch>Publish a branch and set its upstream
git pushPush the current branch
git push --force-with-leaseSafer force push for your own branch only
git remote add upstream <url>Add the original repo when working from a fork

Look around

CommandWhat it does
git log --oneline --graph --allCompact 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 --stagedUnstaged / staged changes
git diff main...featureWhat the feature branch adds since it split
git show <commit>One commit in full
git blame <file>Who last changed each line
git shortlog -snCommit count per author

Undo and recover

CommandWhat 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~1Undo last commit, keep changes staged
git reset --mixed HEAD~1Undo last commit, keep changes unstaged
git reset --hard <commit>Move the branch and throw away changes. Danger
git clean -fdDelete untracked files and folders. Use -n first
git reflogList where HEAD has been, to find lost commits
git switch -c rescue <id>Bring a lost commit back on a new branch

Stash, tags and worktrees

CommandWhat it does
git stash push -u -m "wip"Shelve changes, including untracked files
git stash list / pop / applySee, restore and drop / restore and keep
git tag -a v1.2.0 -m "msg"Create an annotated release tag
git push origin --tagsPublish tags
git worktree add ../fix hotfix/xCheck out another branch in a second folder

Debugging

CommandWhat it does
git bisect start / bad / goodBinary-search for the commit that broke something
git bisect run <test>Let a script do the bisecting
git bisect resetFinish and return to your branch
git grep "text"Search tracked files quickly

.gitignore patterns

PatternMeaning
*.logIgnore all files ending in .log
build/Ignore a folder named build anywhere
/secret.txtIgnore only in the repository root
!keep.logDo 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?

SituationUse
Web app or API, deploy oftenGitHub Flow, or trunk-based with feature flags
Numbered versions, old versions supportedGit Flow, or release/x.y branches
Mobile app with store reviewTrunk + a release branch per store build, hotfix off the release
Open sourceFork and pull request
Production firehotfix/* from the production tag, then merge back to main

Rules of thumb

← Back to all writing