Git Commands Cheat Sheet 2026 — Reference with Examples

By Jay, editor

Published: March 15, 2026 · Updated: October 7, 2026 · 10 min read

Table of Contents

  1. Setup & Configuration
  2. Daily Workflow
  3. Branching & Merging
  4. Remote Repositories
  5. Undoing Mistakes
  6. Stashing
  7. Rebasing
  8. Tags & Releases
  9. History & Diffs
  10. Advanced Tips

Git is a distributed version control system. This cheat sheet covers the commands used most often — from first-time setup to interactive rebasing — with copy-paste ready examples.

1. Setup & Configuration

CommandWhat it does
git config --global user.name "Your Name"Set your display name for commits
git config --global user.email "you@example.com"Set your email for commits
git config --global core.editor "code --wait"Set VS Code as default editor
git config --global init.defaultBranch mainUse main as default branch name
git config listView all current config values (older spelling: --list)
git initInitialize a new repo in current directory
git clone <url>Clone a remote repo locally
git clone --branch <name> --single-branch <url>Clone only the history of one branch

2. Daily Workflow

The everyday loop is four commands: git pull → edit → git add . → git commit -m "msg" → git push.

CommandWhat it does
git statusShow working tree status
git add .Stage everything under the current directory
git add -pStage changes interactively (hunk by hunk)
git commit -m "message"Commit staged changes
git commit -am "message"Stage tracked files and commit in one step
git commit --amendModify the last commit
git push -u origin <branch>Push and set upstream tracking
git pullFetch and integrate remote changes (fast-forward only by default)
git fetchDownload remote changes without merging
Tip: Use git commit --amend --no-edit to add forgotten files to the last commit without changing the message.

3. Branching & Merging

CommandWhat it does
git branchList local branches
git branch -aList all branches including remote
git checkout -b <name>Create and switch to a new branch
git switch -c <name>Modern syntax: create and switch
git branch -d <name>Delete a merged branch (safe)
git branch -D <name>Force-delete a branch
git merge <branch>Merge branch into current branch
git merge --no-ff <branch>Merge with a merge commit
git merge --squash <branch>Stage the branch as one combined change, without a commit or merge record
git cherry-pick <hash>Apply a specific commit to current branch

4. Remote Repositories

CommandWhat it does
git remote -vList remote connections with URLs
git remote add origin <url>Add a remote named "origin"
git remote set-url origin <url>Change remote URL
git push origin --delete <branch>Delete a remote branch
git fetch --pruneRemove stale remote-tracking branches
git push --force-with-leaseSafer force push
Avoid git push --force on shared branches. Use --force-with-lease instead.

5. Undoing Mistakes

CommandWhat it does
git restore <file>Discard unstaged changes to a file
git restore --staged <file>Unstage a file
git reset --soft HEAD~1Undo last commit, keep changes staged
git reset HEAD~1Undo last commit, keep changes unstaged
git reset --hard HEAD~1Undo last commit and discard all changes
git revert <commit>Create a new commit undoing a previous one (safe for shared branches)
git clean -fdRemove untracked files and directories (ignored files stay)
git reflogView recent HEAD movements — your recovery safety net
Recovery: Lost a commit to git reset --hard? Run git reflog to find its hash, then git reset --hard <hash> to go back to it. Uncommitted changes that the reset discarded are not in the reflog.

6. Stashing

CommandWhat it does
git stashStash current changes
git stash -uStash including untracked files
git stash push -m "description"Stash with a description
git stash listShow all stashes
git stash popRestore most recent stash and delete it
git stash apply stash@{2}Restore a specific stash (keeps it)
git stash drop stash@{1}Delete a specific stash
git stash clearDelete all stashes

7. Rebasing

Rebase rewrites history for a linear timeline. Use on local branches only.

CommandWhat it does
git rebase mainReplay current branch commits on top of main
git rebase -i HEAD~5Interactive rebase: edit, squash, reorder, drop last 5 commits
git rebase --continueContinue after resolving a conflict
git rebase --abortCancel the rebase

8. Tags & Releases

CommandWhat it does
git tag -a v1.0.0 -m "Release 1.0"Create an annotated tag
git push origin --tagsPush all tags to remote
git tag -d v1.0.0Delete a local tag
git push origin --delete v1.0.0Delete a remote tag

9. History & Diffs

CommandWhat it does
git log --oneline --graph --allVisual branch graph in terminal
git log -p <file>Show all changes to a specific file
git diff --stagedShow staged changes (what will be committed)
git diff main..featureCompare two branches
git blame <file>Show who changed each line and when

10. Advanced Tips

CommandWhat it does
git log -S"function_name"Find commits that added/removed a specific string
git bisect startBinary search to find the commit that introduced a bug
git submodule update --init --recursiveInitialize and update all submodules
git config --global alias.lg "log --oneline --graph --all"Create a shortcut alias

Also explore: the longer Git command reference on this site

View Git Shortcuts Reference

The model behind the commands

Git's commands make sense once three places are clear: the working tree (files on disk), the index or staging area (what the next commit will contain) and the repository (commits). add moves changes from the tree to the index, commit from the index to the repository, and the undo commands in the sheet are named by which of those they touch — restore for the tree, restore --staged for the index, reset and revert for commits. Reading the sheet with that map removes most of the guesswork.

Safe versus destructive

Commands that only add history — commit, merge, revert, tag, push without force — are safe to try. The ones that rewrite or discard — reset --hard, rebase, push --force, checkout -- file, clean — are the ones to run with a clear head; the sheet carries a warning for the force push and a note on rebase. Commits dropped by a reset or a rebase can usually be found again through reflog, whose entries for unreachable commits are kept 30 days by default; it closes the Undoing Mistakes table here and is the first place to look after a mistake.

Where this goes next

The git page on this site has a longer list of commands with FAQs and a source reference, and the FAQs there answer how to undo the last commit, what a stash stores and how to restore it, and when to rebase instead of merge. Aliases in ~/.gitconfig are the usual next step once the sheet is familiar.