All cheatsheets

Cheatsheets

Git

Common git commands for branching, remotes, undoing, and inspecting history.

42 entries

Setup & config6

git init

Create 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.com

Set your committer email

[alias] co = checkout st = status lg = log --oneline --graph

Handy aliases for ~/.gitconfig

Stage & commit7

git status -sb

Short, branch-aware status

git add <file>

Stage changes for the next commit

git add -p

Interactively stage hunks

git commit -m "msg"

Record staged changes

git commit -am "msg"

Stage tracked files and commit

git commit --amend

Rewrite the most recent commit

git commit --amend --no-edit

Amend without changing the message

Branches8

git branch

List 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~3

Interactively 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 -v

List remotes with URLs

git fetch --all --prune

Fetch all remotes, prune stale refs

git pull --rebase

Fetch and rebase local commits on top

git push

Update remote refs

git push -u origin <branch>

Push and set upstream

git push --force-with-lease

Safer 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~1

Undo 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 reflog

History of HEAD — recover lost commits

git stash

Stash away local changes

git stash pop

Reapply and drop the latest stash

Inspect & search7

git log --oneline --graph --all

Compact commit graph

git log -p <file>

History with diffs for a file

git diff

Show unstaged changes

git diff --staged

Show staged changes

git blame <file>

Who changed each line

git bisect start

Binary-search for a bad commit

git log -S"text"

Find commits that add/remove text