GitPrintable

Git Cheat Sheet

Setup, commits, history, branches, undoing, remotes, rebase and recovery. The Git commands you'll actually use, each with a one-line explanation.

Back to the Git tutorial

Setup

CommandWhat it doesLesson
git --versionCheck Git is installedIntroduction
git config --global user.name "Name"Set your nameIntroduction
git config --global user.email "you@x.com"Set your emailIntroduction
git help <command>Open helpIntroduction

Saving work

CommandWhat it doesLesson
git initStart a repositoryCommits
git statusWhat's changed and stagedCommits
git add <file> / git add .Stage changesCommits
git commit -m "Message"Save a snapshotCommits

History

CommandWhat it doesLesson
git log --onelineShort list of commitsHistory
git log --oneline --graph --allBranch diagramHistory
git diffUnstaged changesHistory
git diff --stagedWhat you're about to commitHistory
git show <hash>One commit in detailHistory
.gitignoreFiles Git should never trackHistory

Moving & removing files

CommandWhat it doesLesson
git mv old newRename or move, stagedMoving & Removing Files
git rm fileDelete and stage the deletionMoving & Removing Files
git rm --cached fileStop tracking, keep on diskMoving & Removing Files
git clean -nd / git clean -fdPreview / delete untracked filesMoving & Removing Files
git ls-filesList tracked filesMoving & Removing Files

Configuration & aliases

CommandWhat it doesLesson
git config --list --show-originEvery setting and where it comes fromConfig & Aliases
git config --global init.defaultBranch mainName new branches mainConfig & Aliases
git config --global core.editor "code --wait"Choose your editorConfig & Aliases
git config --global alias.st statusCreate a shortcut: git stConfig & Aliases
git config --global pull.rebase trueRebase instead of merge on pullConfig & Aliases

Branches & merging

CommandWhat it doesLesson
git branchList branchesBranches
git switch -c <name>Create and switchBranches
git switch <name>Change branchBranches
git branch -d <name>Delete a merged branchBranches
git merge <branch>Merge into current branchMerging
git merge --abortCancel a mergeMerging

Merge conflicts

CommandWhat it doesLesson
git checkout --ours fileKeep your versionMerge Conflicts
git checkout --theirs fileKeep the incoming versionMerge Conflicts
git add file then git commitMark resolved and finishMerge Conflicts
git config --global merge.conflictStyle zdiff3Show the original lines in conflictsMerge Conflicts

Undoing

CommandWhat it doesLesson
git restore <file>Discard uncommitted editsUndoing
git restore --staged <file>UnstageUndoing
git commit --amendFix the last (unpushed) commitUndoing
git revert <hash>Undo a commit with a new commitUndoing
git reset --soft HEAD~1Remove last commit, keep changesUndoing

Stashing

CommandWhat it doesLesson
git stash push -m "note"Shelve changes with a descriptionStash
git stash -uInclude untracked filesStash
git stash listShow all stashesStash
git stash popReapply the latest and remove itStash
git stash apply stash@{1}Reapply a stash but keep itStash
git stash drop stash@{0}Delete one stashStash

Remotes

CommandWhat it doesLesson
git clone <url>Copy a remote repositoryRemotes
git remote -vList remotes and URLsRemotes
git remote add origin <url>Connect a remoteRemotes
git push -u origin <branch>Push and set upstreamRemotes
git fetchDownload without mergingRemotes
git pullFetch and mergeRemotes

Tags

CommandWhat it doesLesson
git tagList tagsTags
git tag -a v1.0.0 -m "Message"Create an annotated tagTags
git push origin v1.0.0Push one tagTags
git push --tagsPush all tagsTags
git tag -d v1.0.0Delete a local tagTags

Rebase & cherry-pick

CommandWhat it doesLesson
git rebase mainReplay your branch on top of mainRebase
git rebase -i HEAD~3Reword, squash or drop recent commitsRebase
git rebase --continue / --abortCarry on or cancel a rebaseRebase
git push --force-with-leaseSafely update your rebased branchRebase
git cherry-pick <hash>Copy one commit onto this branchCherry-Pick
git cherry-pick -x <hash>Copy and record the source commitCherry-Pick

Recovery & investigation

CommandWhat it doesLesson
git reflogEvery recent move of HEADReflog
git reset --hard HEAD@{1}Go back to before the last moveReflog
git branch <name> <hash>Restore a deleted branchReflog
git bisect start / good / badBinary search for a bad commitBisect & Blame
git bisect run <command>Automate the searchBisect & Blame
git blame -L 10,20 <file>Who last changed each lineBisect & Blame
git log -S "text"Commits that added or removed textBisect & Blame

Team workflow

CommandWhat it doesLesson
git switch main && git pullStart from the latest mainWorkflows
git switch -c feature/nameOne branch per taskWorkflows
git commit -m "feat: summary"Conventional Commits messageWorkflows
git branch -d feature/nameTidy up after mergingWorkflows

Hooks, submodules & worktrees

CommandWhat it doesLesson
chmod +x .git/hooks/pre-commitEnable a hookGit Hooks
git config core.hooksPath .githooksShare hooks with the teamGit Hooks
git commit --no-verifySkip commit hooks onceGit Hooks
git submodule add <url> <path>Add a submoduleSubmodules
git clone --recurse-submodules <url>Clone with submodulesSubmodules
git submodule update --remoteUpdate a submoduleSubmodules
git worktree add ../dir -b branch mainSecond folder, new branchWorktrees
git worktree list / removeList / remove worktreesWorktrees

How to use this Git cheat sheet

This Git cheat sheet collects the commands you'll use most, grouped in the same order as the course: setup and configuration, saving work, history, managing files, branches, merging and conflicts, undoing, stashing, remotes, tags, rebasing, cherry-picking, recovery, investigation, hooks, submodules and worktrees. Each row has a one-line explanation and a link to the lesson that covers it with full examples and common mistakes.

Keep it open while you work through the lessons or your own projects. When you can't remember whether it's git restore --staged or git reset that unstages a file, or how to include the first commit in a cherry-pick range, a quick look here is faster than a search. Type part of a command into the filter box to jump straight to it, or print the page and keep it beside your keyboard.

Git gotchas worth remembering

  • Stage before you commit. git commit only saves what git add staged. Check with git status first.
  • reset --hard throws work away. Uncommitted changes can't be recovered. Use git stash if you might want them back.
  • Don't rebase shared branches. Rewriting commits others have pulled forces everyone to repair their history. Rebase only your own local work.

Try each command in a throwaway repository before using it on real work, then check yourself with the Git quiz.

The Git lessons behind this cheat sheet