Git Cheatsheet

Comprehensive Git cheatsheet covering setup, branching, remotes, stashing, undoing changes, tags, bisect, and log tricks.

Updated September 15, 2026

gitversion-controlgithubbranching

Setup

git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global core.editor vim
git config --global init.defaultBranch main
git config --list

Advertisement

Repository Basics

CommandAction
git initInit repo in current dir
git clone <url>Clone remote repo
git clone <url> dirClone into specific directory
git statusShow working tree status
git logShow commit history
git log --onelineCompact log
git log --oneline --graph --allVisual branch graph
git log --since="2 weeks ago"Filter by date
git log -pShow diff per commit
git show <commit>Show a specific commit
git diffUnstaged changes
git diff --stagedStaged changes
git diff <branch1>..<branch2>Diff between branches

Staging and Committing

CommandAction
git add <file>Stage specific file
git add .Stage all changes
git add -pStage hunks interactively
git commit -m "message"Commit staged changes
git commit -am "message"Stage tracked files and commit
git commit --amendAmend last commit (message or files)
git commit --amend --no-editAmend last commit keeping message

Branching

CommandAction
git branchList local branches
git branch -aList all branches (inc. remote)
git branch <name>Create branch
git branch -d <name>Delete branch (safe)
git branch -D <name>Force delete branch
git switch <name>Switch to branch
git switch -c <name>Create and switch
git checkout -b <name>Create and switch (older syntax)
git merge <branch>Merge branch into current
git merge --no-ff <branch>Merge with merge commit
git merge --squash <branch>Squash all commits into one staged
git rebase <branch>Rebase current branch onto branch
git rebase -i HEAD~3Interactive rebase last 3 commits

Advertisement

Remotes

CommandAction
git remote -vList remotes
git remote add origin <url>Add remote
git remote set-url origin <url>Change remote URL
git fetch originFetch all remote changes
git fetch origin <branch>Fetch specific branch
git pullFetch and merge current branch
git pull --rebaseFetch and rebase
git push origin <branch>Push branch to remote
git push -u origin <branch>Push and set upstream
git push --force-with-leaseSafe force push
git push origin --delete <branch>Delete remote branch

Stashing

CommandAction
git stashStash working directory
git stash push -m "name"Stash with name
git stash listList stashes
git stash popApply and drop top stash
git stash apply stash@{2}Apply specific stash (keep it)
git stash drop stash@{0}Delete specific stash
git stash clearDelete all stashes
git stash branch <name>Create branch from stash

Undoing Changes

CommandAction
git restore <file>Discard unstaged changes in file
git restore --staged <file>Unstage file (keep changes)
git reset HEAD~1Undo last commit, keep changes staged
git reset --soft HEAD~1Undo last commit, keep changes staged
git reset --mixed HEAD~1Undo last commit, unstage changes
git reset --hard HEAD~1Undo last commit, discard changes
git revert <commit>Create new commit that undoes commit
git clean -fdDelete untracked files and dirs
git clean -nDry run: show what would be deleted

Cherry-Pick

git cherry-pick <commit-hash>          # apply single commit
git cherry-pick <hash1>..<hash2>       # apply range
git cherry-pick --no-commit <hash>     # apply without committing

Tags

CommandAction
git tagList tags
git tag v1.0Create lightweight tag
git tag -a v1.0 -m "msg"Create annotated tag
git push origin v1.0Push specific tag
git push origin --tagsPush all tags
git tag -d v1.0Delete local tag
git push origin --delete v1.0Delete remote tag

Bisect

git bisect start
git bisect bad                  # current commit is bad
git bisect good <commit>        # last known good commit
# git checks out midpoint - test it, then:
git bisect good                 # or: git bisect bad
# repeat until found
git bisect reset                # return to original HEAD

Useful Shortcuts

git log --all --decorate --oneline --graph   # full visual history
git log --author="name" --oneline            # commits by author
git shortlog -sn                             # commit count by author
git blame <file>                             # who changed each line
git grep "pattern"                           # search working tree
git reflog                                   # history of HEAD movements (recovery tool)
ToolsTagsRSSPrivacy PolicyTerms