Git Cheatsheet

Comprehensive Git command reference with 150+ commands organized by category. Search, filter by category, and click to copy any command.

Git Cheatsheet

Essential Git commands for everyday use

104

Commands

10

Categories

0

Favorites

23

Sections

Setup
Configuration
git config --global user.name '[name]'

Set global username

git config --global user.email '[email]'

Set global email

git config --global core.editor 'code --wait'

Set VS Code as default editor

git config --global init.defaultBranch main

Set default branch name

git config --list

List all config settings

git config --global alias.co checkout

Create alias 'co' for checkout

Setup
Initialize
git init

Initialize new repository

git clone [url]

Clone remote repository

git clone --depth 1 [url]

Shallow clone (latest only)

git clone --branch [branch] [url]

Clone specific branch

Basic
Stage & Commit
git status

Show working tree status

git add [file]

Stage specific file

git add .

Stage all changes

git add -p

Stage changes interactively

git commit -m '[message]'

Commit with message

git commit -am '[message]'

Stage and commit tracked files

git commit --amend

Amend last commit

git commit --amend -m '[new message]'

Change last commit message

Basic
File Operations
git rm [file]

Remove file from repo and disk

git rm --cached [file]

Remove from repo, keep on disk

git mv [old] [new]

Rename/move file

git restore [file]

Restore file (modern)

git restore --staged [file]

Unstage file (modern)

Branching
Branch Management
git branch

List local branches

git branch -a

List all branches

git branch [name]

Create new branch

git branch -d [name]

Delete branch (safe)

git branch -D [name]

Delete branch (force)

git branch -m [old] [new]

Rename branch

Branching
Switch & Checkout
git checkout [branch]

Switch to branch

git checkout -b [name]

Create and switch to branch

git switch [branch]

Switch branch (modern)

git switch -c [name]

Create and switch (modern)

git checkout -

Switch to previous branch

Merge & Rebase
Merging
git merge [branch]

Merge branch into current

git merge --no-ff [branch]

Merge with merge commit

git merge --squash [branch]

Squash merge (no commit)

git merge --abort

Abort merge in progress

Merge & Rebase
Rebasing
git rebase [branch]

Rebase onto branch

git rebase -i HEAD~[n]

Interactive rebase last n commits

git rebase --continue

Continue after resolving conflicts

git rebase --abort

Abort rebase

💡 Never rebase commits pushed to shared repository

Merge & Rebase
Cherry Pick
git cherry-pick [commit]

Apply commit to current branch

git cherry-pick --no-commit [commit]

Apply without committing

Remote
Remote Management
git remote -v

List remotes with URLs

git remote add [name] [url]

Add remote

git remote remove [name]

Remove remote

git remote set-url [name] [url]

Change remote URL

Remote
Fetch & Pull
git fetch

Fetch all remotes

git fetch --prune

Fetch and remove deleted remote branches

git pull

Fetch and merge

git pull --rebase

Fetch and rebase

Remote
Push
git push

Push to tracked 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

git push --tags

Push all tags

Undo
Reset
git reset [file]

Unstage file

git reset --soft HEAD~1

Undo commit, keep staged

git reset --mixed HEAD~1

Undo commit, keep changes

git reset --hard HEAD~1

Undo commit, discard changes

git reset --hard origin/[branch]

Reset to remote branch

💡 --soft keeps staged, --mixed unstages, --hard discards

Undo
Revert & Clean
git revert [commit]

Create commit that undoes changes

git clean -n

Preview untracked files to remove

git clean -fd

Remove untracked files and directories

Undo
Recovery
git reflog

Show reference log

git checkout [reflog-hash]

Recover from reflog

💡 Reflog keeps history for ~90 days

Inspect
Log
git log

Show commit history

git log --oneline

Compact log

git log --graph --oneline --all

Full graph view

git log --author='[name]'

Filter by author

git log -S '[text]'

Search commits by content

Inspect
Diff
git diff

Show unstaged changes

git diff --staged

Show staged changes

git diff [branch1]..[branch2]

Compare branches

git diff --name-only

Show changed file names

Inspect
Show & Blame
git show [commit]

Show commit details

git blame [file]

Show who changed each line

git shortlog -sn

Commit count by author

Stash
Stash Operations
git stash

Stash changes

git stash push -m '[message]'

Stash with message

git stash -u

Stash including untracked

git stash list

List all stashes

git stash show -p

Show stash diff

git stash pop

Apply and remove latest stash

git stash apply

Apply latest stash

git stash drop

Remove latest stash

git stash clear

Remove all stashes

Tags
Tag Operations
git tag

List all tags

git tag [name]

Create lightweight tag

git tag -a [name] -m '[message]'

Create annotated tag

git tag -d [name]

Delete local tag

git push origin [tag]

Push tag to remote

git push origin --delete [tag]

Delete remote tag

Advanced
Submodules
git submodule add [url]

Add submodule

git submodule update --init --recursive

Init and update all

git clone --recurse-submodules [url]

Clone with submodules

Advanced
Worktrees
git worktree add [path] [branch]

Create worktree

git worktree list

List worktrees

git worktree remove [path]

Remove worktree

Advanced
Bisect
git bisect start

Start bisect session

git bisect bad

Mark current as bad

git bisect good [commit]

Mark commit as good

git bisect reset

End bisect session

💡 Binary search to find bug-introducing commit

Git Workflow Quick Reference

1. Start

git init / clone

2. Stage

git add .

3. Commit

git commit -m ""

4. Push

git push

5. Pull

git pull

What is Git?

Git is a distributed version control system that tracks changes in source code during software development. It allows multiple developers to work together on non-linear development through branches, and provides tools for merging changes, tracking history, and collaborating on projects.

Common Git Workflows

  • Feature Branch Workflow - Create branches for new features, merge when complete
  • Gitflow - Structured branching model with develop, feature, release, and hotfix branches
  • Trunk-Based Development - Short-lived branches merged frequently to main
  • Forking Workflow - Fork repositories, make changes, submit pull requests

Best Practices

  • Write clear, descriptive commit messages
  • Commit early and often with small, focused changes
  • Use branches for features, fixes, and experiments
  • Pull before pushing to avoid conflicts
  • Never force push to shared branches
  • Use .gitignore to exclude unnecessary files