← All sheets

Git

Everyday commands, branching, and how to undo almost anything.

Setup

Command Description
git config --global user.name "Name" Set your name
git config --global user.email "you@example.com" Set your email
git config --global init.defaultBranch main Default branch for new repos
git config --global pull.rebase true Rebase instead of merge on pull
git config --list --show-origin Show all settings and where they come from

Starting

Command Description
git init Create a repo in the current folder
git clone <url> Clone a remote repo
git clone --depth 1 <url> Shallow clone (latest commit only)

Daily workflow

Command Description
git status -sb Short status with branch info
git add <file> Stage a file
git add -p Stage changes hunk by hunk
git commit -m "msg" Commit staged changes
git commit -am "msg" Stage tracked files and commit
git pull Fetch and integrate remote changes
git push Push current branch
git push -u origin <branch> Push and set upstream

Branches

Command Description
git switch <branch> Switch branch
git switch -c <branch> Create and switch
git branch -a List local and remote branches
git branch -d <branch> Delete merged branch
git branch -D <branch> Force delete branch
git merge <branch> Merge branch into current
git rebase main Replay current branch on top of main
git push origin --delete <branch> Delete remote branch

Inspecting

Command Description
git log --oneline --graph --all Compact history graph
git log -p <file> History of a file with diffs
git diff Unstaged changes
git diff --staged Staged changes
git show <commit> Show a commit
git blame <file> Who changed each line
git log -S "text" Find commits that added/removed text

Undoing things

Command Description
git restore <file> Discard unstaged changes in a file
git restore --staged <file> Unstage a file
git commit --amend Edit the last commit
git reset --soft HEAD~1 Undo last commit, keep changes staged
git reset --hard HEAD~1 Undo last commit and discard changes
git revert <commit> New commit that undoes a commit
git reflog Every position HEAD has been, to recover lost work

Stash

Command Description
git stash Stash uncommitted changes
git stash -u Include untracked files
git stash list List stashes
git stash pop Apply and drop latest stash
git stash drop Drop latest stash

Remotes

Command Description
git remote -v List remotes
git remote add origin <url> Add a remote
git fetch --prune Fetch and remove deleted remote branches
git cherry-pick <commit> Apply a single commit to current branch