Cheatsheets
Git
Common git commands for branching, remotes, undoing, and inspecting history.
42 entries
Setup & config6
git initCreate an empty Git repository
git clone <url>Clone a repository into a new directory
git clone --depth 1 <url>Shallow clone (latest commit only)
git config --global user.name "You"Set your committer name
git config --global user.email you@x.comSet your committer email
[alias]
co = checkout
st = status
lg = log --oneline --graphHandy aliases for ~/.gitconfig
Stage & commit7
git status -sbShort, branch-aware status
git add <file>Stage changes for the next commit
git add -pInteractively stage hunks
git commit -m "msg"Record staged changes
git commit -am "msg"Stage tracked files and commit
git commit --amendRewrite the most recent commit
git commit --amend --no-editAmend without changing the message
Branches8
git branchList branches
git switch -c <name>Create and switch to a new branch
git switch <name>Switch to an existing branch
git merge <branch>Merge a branch into the current one
git rebase <branch>Reapply commits on top of another base
git rebase -i HEAD~3Interactively rewrite the last 3 commits
git cherry-pick <ref>Apply a commit from elsewhere
git branch -d <name>Delete a merged branch
Remote6
git remote -vList remotes with URLs
git fetch --all --pruneFetch all remotes, prune stale refs
git pull --rebaseFetch and rebase local commits on top
git pushUpdate remote refs
git push -u origin <branch>Push and set upstream
git push --force-with-leaseSafer force-push (won't clobber others)
Undo & recover8
git restore <file>Discard working-tree changes
git restore --staged <file>Unstage a file
git reset --soft HEAD~1Undo last commit, keep changes staged
git reset --hard <ref>Discard changes and move HEAD
git revert <ref>Create a commit that undoes a commit
git reflogHistory of HEAD — recover lost commits
git stashStash away local changes
git stash popReapply and drop the latest stash
Inspect & search7
git log --oneline --graph --allCompact commit graph
git log -p <file>History with diffs for a file
git diffShow unstaged changes
git diff --stagedShow staged changes
git blame <file>Who changed each line
git bisect startBinary-search for a bad commit
git log -S"text"Find commits that add/remove text