Git Cheatsheet
Comprehensive Git cheatsheet covering setup, branching, remotes, stashing, undoing changes, tags, bisect, and log tricks.
Updated September 15, 2026
gitversion-controlgithubbranching
Setup
bash
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
| Command | Action |
|---|---|
git init | Init repo in current dir |
git clone <url> | Clone remote repo |
git clone <url> dir | Clone into specific directory |
git status | Show working tree status |
git log | Show commit history |
git log --oneline | Compact log |
git log --oneline --graph --all | Visual branch graph |
git log --since="2 weeks ago" | Filter by date |
git log -p | Show diff per commit |
git show <commit> | Show a specific commit |
git diff | Unstaged changes |
git diff --staged | Staged changes |
git diff <branch1>..<branch2> | Diff between branches |
Staging and Committing
| Command | Action |
|---|---|
git add <file> | Stage specific file |
git add . | Stage all changes |
git add -p | Stage hunks interactively |
git commit -m "message" | Commit staged changes |
git commit -am "message" | Stage tracked files and commit |
git commit --amend | Amend last commit (message or files) |
git commit --amend --no-edit | Amend last commit keeping message |
Branching
| Command | Action |
|---|---|
git branch | List local branches |
git branch -a | List 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~3 | Interactive rebase last 3 commits |
Advertisement
Remotes
| Command | Action |
|---|---|
git remote -v | List remotes |
git remote add origin <url> | Add remote |
git remote set-url origin <url> | Change remote URL |
git fetch origin | Fetch all remote changes |
git fetch origin <branch> | Fetch specific branch |
git pull | Fetch and merge current branch |
git pull --rebase | Fetch and rebase |
git push origin <branch> | Push branch to remote |
git push -u origin <branch> | Push and set upstream |
git push --force-with-lease | Safe force push |
git push origin --delete <branch> | Delete remote branch |
Stashing
| Command | Action |
|---|---|
git stash | Stash working directory |
git stash push -m "name" | Stash with name |
git stash list | List stashes |
git stash pop | Apply and drop top stash |
git stash apply stash@{2} | Apply specific stash (keep it) |
git stash drop stash@{0} | Delete specific stash |
git stash clear | Delete all stashes |
git stash branch <name> | Create branch from stash |
Undoing Changes
| Command | Action |
|---|---|
git restore <file> | Discard unstaged changes in file |
git restore --staged <file> | Unstage file (keep changes) |
git reset HEAD~1 | Undo last commit, keep changes staged |
git reset --soft HEAD~1 | Undo last commit, keep changes staged |
git reset --mixed HEAD~1 | Undo last commit, unstage changes |
git reset --hard HEAD~1 | Undo last commit, discard changes |
git revert <commit> | Create new commit that undoes commit |
git clean -fd | Delete untracked files and dirs |
git clean -n | Dry run: show what would be deleted |
Cherry-Pick
bash
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
| Command | Action |
|---|---|
git tag | List tags |
git tag v1.0 | Create lightweight tag |
git tag -a v1.0 -m "msg" | Create annotated tag |
git push origin v1.0 | Push specific tag |
git push origin --tags | Push all tags |
git tag -d v1.0 | Delete local tag |
git push origin --delete v1.0 | Delete remote tag |
Bisect
bash
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
bash
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)