Skip to content

Cheat sheet: Git

Git history, undo, branches and remotes, every flag explained.

Index
  • #cheatsheet
  • #git
Updated

Also:git log · reset · rebase · stash · force-with-lease

Each entry shows the command, a copy button, and what every part does: sub-command, flag, pipe, redirect and placeholder, in the order they appear. <placeholders> are the bits you replace.

71 items
Click a command to see what each part does.

Basics: set up, stage, commit, sync23

  • git config --global user.name "<name>"; git config --global user.email "<email>"Set your name and email (once per machine)

    What each part does

    1. git configread/write Git settings
    2. --globalfor your user (~/.gitconfig), all repos
    3. user.name / user.emailrecorded as the author of your commits

    Tip Git 2.46+ also has: git config set --global user.name "<name>". Check with git config list --show-origin.

  • git init -b mainStart a new repo

    What each part does

    1. initcreate a .git folder here
    2. -b main--initial-branch: name the first branch main
  • git clone <url>Copy a remote repo

    What each part does

    1. clone <url>download the repo with full history into a new folder
  • git clone --depth 1 <url>Shallow clone (fast, latest commit only)

    What each part does

    1. --depth 1only the most recent commit, no history
  • git statusWhat changed?

    What each part does

    1. statusbranch, staged, not-staged and untracked files
  • git add <file>; git add -AStage one file / everything

    What each part does

    1. add <file>stage that file for the next commit
    2. add -A--all: stage every change in the repo, including deletions (git add . = only under the current folder)
  • git add -pStage only parts of a file

    What each part does

    1. -p--patch: go through each change and choose y/n
  • git commit -m "<message>"Commit staged changes

    What each part does

    1. commitrecord the staged changes
    2. -m "<message>"commit message (without -m an editor opens)
  • git commit -am "<message>"Stage tracked files and commit in one step

    What each part does

    1. -a--all: auto-stage modified and deleted tracked files (NOT new files)
    2. -mmessage
  • git commit --amend --no-editFix the last commit (not pushed yet)Caution

    What each part does

    1. --amendreplace the last commit with one that includes what is staged now
    2. --no-editkeep the same message
  • git log --oneline -n 10Recent history

    What each part does

    1. --onelineone line per commit
    2. -n 10only the last 10
  • git diff; git diff --stagedSee the changes

    What each part does

    1. git diffchanges not staged yet
    2. --stagedwhat is staged (alias --cached)
  • git show <commit>Show one commit

    What each part does

    1. showmessage, author and diff of that commit (HEAD if omitted)
  • git branch -aList branches

    What each part does

    1. -a--all: local and remote-tracking branches; * marks the current one
  • git checkout -b <branch>Create and switch to a branch

    What each part does

    1. checkout -bolder equivalent of git switch -c
  • git branch -d <branch>; git branch -D <branch>Delete a branchCaution

    What each part does

    1. -ddelete if it is fully merged
    2. -Dforce delete even if not merged
  • git merge --no-ff <branch>Merge a branch into the current one

    What each part does

    1. merge <branch>bring <branch>'s commits into the current branch
    2. --no-ffalways create a merge commit, even when a fast-forward is possible

    Tip Conflict? Fix the files, git add them, git commit, or give up with git merge --abort.

  • git remote -vWhich remotes are set up?

    What each part does

    1. -vshow fetch and push URLs
  • git remote add origin <url>Add a remote

    What each part does

    1. remote add origin <url>name the remote "origin" (convention)
  • git pullGet the latest from the remote

    What each part does

    1. pullfetch + merge the upstream branch (git pull --rebase to rebase instead)
  • git pushUpload your commits

    What each part does

    1. pushsend commits to the upstream branch (first time: git push -u origin <branch>)
  • git rm --cached <file>Stop tracking a file but keep it on disk

    What each part does

    1. rmremove from Git
    2. --cachedonly from the index; the file stays (then add it to .gitignore)
  • git mv <old> <new>Rename a tracked file

    What each part does

    1. git mvrename and stage the rename in one step
  • git log --oneline --graph --decorate --allCompact graph of all branches

    What each part does

    1. git logshow commit history
    2. --onelineone line per commit: short hash + subject
    3. --graphASCII graph of branches and merges
    4. --decorateshow branch and tag names next to commits
    5. --allinclude every branch and tag, not just the current one
  • git log --oneline | grep -i <text>Find commits whose message mentions something

    What each part does

    1. git log --onelineone commit per line
    2. | grep -i <text>keep lines containing <text>, any case

    Tip Built-in: git log --oneline --grep=<text> -i

  • git log --oneline --author=<name> --since=<date> --until=<date>Commits by someone in a date range

    What each part does

    1. --author=<name>author matches (regex)
    2. --since / --untildate range, e.g. "2 weeks ago", 2026-11-01
  • git log -S"<text>" -pWhen was this string added or removed?

    What each part does

    1. -S"<text>""pickaxe": commits that changed how many times <text> appears
    2. -pshow the diff of each matching commit
  • git log --follow -p -- <file>History of one file, across renames

    What each part does

    1. --followkeep following the file when it was renamed
    2. -pwith diffs
    3. -- <file>"--" ends options; what follows is a path
  • git blame -L <start>,<end> -- <file>Who changed these lines

    What each part does

    1. blamelast commit + author for every line
    2. -L <start>,<end>only that line range
  • git status -sb; git diff; git diff --stagedWhat is staged vs not

    What each part does

    1. status -sbshort status with branch info
    2. git diffunstaged changes
    3. git diff --stagedwhat will go into the next commit

Undo7

  • git restore --staged <file>Unstage a file (keep the changes)

    What each part does

    1. restorerestore files
    2. --stagedonly the index (staging area): the change goes back to "not staged"
  • git restore <file>Discard local changes to a fileDestructive

    What each part does

    1. restore <file>replace the working copy with the last committed/staged version
  • git reset --soft HEAD~1Undo the last commit, keep the changes stagedCaution

    What each part does

    1. resetmove the current branch pointer
    2. --softonly move HEAD; index and files are untouched
    3. HEAD~1one commit before HEAD
  • git fetch origin && git reset --hard origin/<branch>Throw away local commits and changes to match the remoteDestructive

    What each part does

    1. git fetch origindownload the latest remote state
    2. &&only if that worked
    3. reset --hardmove the branch AND overwrite index + working tree
    4. origin/<branch>the remote version
  • git revert <commit>Undo a pushed commit safely

    What each part does

    1. revert <commit>create a new commit that undoes <commit>; history is not rewritten
  • git reflog -n 20Find a "lost" commit

    What each part does

    1. reflogevery place HEAD has been (commits, resets, rebases)
    2. -n 20last 20 entries; then git branch rescue <sha>
  • git stash push -u -m "<message>"; git stash popPark work in progress

    What each part does

    1. stash pushsave changes and clean the working tree
    2. -uinclude untracked files
    3. -m "<message>"label it
    4. stash popapply the latest stash and drop it

Branches & remotes10

  • git switch -c <branch>Create and switch to a branch

    What each part does

    1. switchchange branches
    2. -c <branch>create it first
  • git branch --merged main | grep -vE '^\*|main' | xargs -r git branch -dDelete local branches already merged into mainCaution

    What each part does

    1. git branch --merged mainbranches fully merged into main
    2. grep -vE '^\*|main'-v: drop lines matching; -E: extended regex: the current branch (starts with *) and main itself
    3. xargs -r git branch -ddelete each remaining branch; -d refuses unmerged ones
  • git fetch --pruneRemove stale remote-tracking branches

    What each part does

    1. fetchdownload from the remote
    2. --prunedelete local refs to branches that no longer exist on the remote
  • git pull --rebase origin mainRebase your branch on the latest main

    What each part does

    1. pullfetch + integrate
    2. --rebasereplay your commits on top instead of creating a merge commit
    3. origin mainremote and branch
  • git push -u origin <branch>Push a new branch and track it

    What each part does

    1. -u--set-upstream: later git push / git pull need no arguments
  • git push --force-with-lease origin <branch>Force-push safely after a rebaseCaution

    What each part does

    1. --force-with-leaseoverwrite the remote branch only if nobody pushed since your last fetch
  • git push origin --delete <branch>Delete a remote branchDestructive

    What each part does

    1. --deleteremove that branch on the remote
  • git cherry-pick <commit>Copy one commit onto the current branch

    What each part does

    1. cherry-pick <commit>apply the changes of <commit> as a new commit here
  • git tag -a <version> -m "<message>" && git push origin <version>Tag a release and push it

    What each part does

    1. tag -a <version>annotated tag (has author, date, message)
    2. -m "<message>"tag message
    3. git push origin <version>tags are not pushed with branches; push it explicitly
  • git clean -nd; git clean -fdPreview / remove untracked filesDestructive

    What each part does

    1. cleanremove untracked files
    2. -ndry run: list what would be removed
    3. -dinclude untracked directories
    4. -factually delete

Rebase, bisect & advanced history12

  • git rebase -i HEAD~<n>Clean up your last commits before a PRCaution

    What each part does

    1. rebase -iinteractive: reorder, squash (s), fixup (f), reword (r) or drop commits
    2. HEAD~<n>the last n commits

    Tip Rewrites history: only for commits not yet shared, or push with --force-with-lease.

  • git fetch origin && git rebase origin/mainRebase your branch on the latest mainCaution

    What each part does

    1. git fetch originget the newest main
    2. git rebase origin/mainreplay your commits on top of it
  • git rebase --continue; git rebase --abort; git merge --abortContinue, skip or abort a rebase / merge

    What each part does

    1. --continueafter fixing conflicts and git add
    2. rebase --abortgo back to before the rebase
    3. merge --abortcancel a conflicted merge
  • git commit --fixup <commit> && git rebase -i --autosquash <commit>~1Squash a fix into an earlier commit automaticallyCaution

    What each part does

    1. commit --fixup <commit>commit marked fixup! for that commit
    2. rebase -i --autosquashmoves and squashes it into place
  • git bisect start <bad> <good>; git bisect good; git bisect bad; git bisect resetFind the commit that introduced a bug

    What each part does

    1. bisect start <bad> <good>binary search between a bad commit (e.g. HEAD) and a known good one
    2. bisect good / badmark each checked-out commit after testing
    3. bisect resetfinish and return to your branch

    Tip git bisect run <test-script> automates it.

  • git log --oneline main..<branch>Commits on a branch that aren't on main

    What each part does

    1. main..<branch>reachable from branch but not from main
  • git diff main...<branch> --statCompare two branches

    What each part does

    1. main...<branch>changes on branch since it split from main (three dots)
    2. --statfiles and line counts only
  • git shortlog -sn --no-mergesWho committed how much

    What each part does

    1. shortlog -s -ncommit count per author, sorted
    2. --no-mergesskip merge commits
  • git grep -n '<text>' <commit-or-branch>Search the code in any commit

    What each part does

    1. git grep -nfast search of tracked files with line numbers
    2. <commit-or-branch>search that version instead of the working tree
  • git restore --source=<commit> -- <file>Restore a file from another commit or branchCaution

    What each part does

    1. --source=<commit>take the file's content from that commit
    2. -- <file>only that path
  • git describe --tags --alwaysDescribe the current version from tags

    What each part does

    1. describe --tagsnearest tag + commits since + short SHA, e.g. v1.4.0-3-g4f2a9c1
    2. --alwaysfall back to the SHA if no tag
  • git filter-repo --invert-paths --path <file>Remove a file from all history (leaked secret)Destructive

    What each part does

    1. git filter-reporewrite the whole history (separate tool, replaces filter-branch)
    2. --invert-paths --path <file>drop that file everywhere

    Tip Rotate the secret first; everyone must re-clone after the force push.

Stashes, tags, remotes, worktrees & submodules12

  • git stash list; git stash show -p stash@{1}; git stash apply stash@{1}; git stash drop stash@{1}List, show, apply and drop stashes

    What each part does

    1. stash listall stashes
    2. show -pthe diff of one
    3. applyre-apply but keep it (pop removes it)
    4. dropdelete it
  • git tag -l 'v1.*'; git tag -d <tag> && git push origin --delete <tag>List and delete tagsCaution

    What each part does

    1. tag -l 'v1.*'list matching tags
    2. tag -ddelete locally
    3. push origin --delete <tag>delete on the remote
  • git push --tagsPush all tags

    What each part does

    1. --tagspush every local tag (git push --follow-tags pushes only annotated ones for pushed commits)
  • git remote set-url origin <url>Change a remote's URL (HTTPS to SSH, moved repo)

    What each part does

    1. remote set-url originpoint origin at a new URL
  • git worktree add ../<dir> <branch>; git worktree list; git worktree remove ../<dir>Work on two branches at once

    What each part does

    1. worktree addcheck out another branch in a second folder, sharing the same repo
    2. list / removesee and clean up worktrees
  • git clone --recurse-submodules <url>; git submodule update --init --recursiveClone with submodules / update them

    What each part does

    1. --recurse-submodulesalso clone nested repos
    2. submodule update --init --recursivefetch submodules in an existing clone
  • git ls-files | grep <pattern>Which files does git track?

    What each part does

    1. ls-filestracked files
    2. | grepfilter
  • git check-ignore -v <file>Why is this file ignored?

    What each part does

    1. check-ignore -vthe .gitignore file and line that ignores it
  • git config --list --show-originSee all config and where it comes from

    What each part does

    1. --listevery setting
    2. --show-originwhich file set it (system, global, repo)
  • git config --global alias.lg "log --oneline --graph --decorate --all"Create a git alias

    What each part does

    1. alias.lgnew command git lg
    2. "log ..."what it expands to
  • git archive --format=zip -o <file>.zip <commit-or-tag>Make a zip of a commit without .git

    What each part does

    1. git archiveexport the tracked files of that version
    2. --format=zip -ooutput format and file
  • git switch -Switch back to the previous branch

    What each part does

    1. -the branch you were on before (like cd -)

My DevOps Playbook by Angelo John Calleja

New runbook

GitHub not connected

Saving is handled by the site: the GitHub token is stored in Cloudflare, not in your browser. Enter the editor password (the EDITOR_PASSWORD secret) to unlock saving in this browser.

Saving commits to gelogrammer/My_DevOps_Playbook. Create a fine-grained token with access to only this repo andContents: Read and write, then paste it here. It is stored only in this browser.

src/content/docs/…