Production Reference

Git Cheat Sheet

Everyday Git workflows, branching, rebasing, recovery, and history inspection commands for engineers working in real repositories.

Command-firstProduction notesSecurity warningsHardened patterns

Setup & Config

6 commands
git config --global user.name "Name"

Set name

git config --global user.email "e@x.com"

Set email

git init

Initialize new repo

git clone URL

Clone remote repo

git remote -v

List remotes

git remote add origin URL

Add remote

Basic Workflow

7 commands
git status

Working tree status

git add file.txt

Stage a file

git add .

Stage all changes

git commit -m 'msg'

Commit staged changes

git push origin main

Push to remote

git pull origin main

Pull latest changes

git fetch origin

Download without merge

Branching

9 commands
git branch

List local branches

git branch -a

List all branches

git branch feature

Create branch

git checkout feature

Switch branch

git checkout -b feature

Create & switch

git switch feature

Switch (modern)

git switch -c feature

Create & switch (modern)

git branch -d feature

Delete merged branch

git branch -D feature

Force delete branch

Merging & Rebasing

6 commands
git merge feature

Merge branch into current

git merge --no-ff feature

Merge with commit

git rebase main

Rebase onto main

git rebase -i HEAD~3

Interactive rebase (squash)

git merge --abort

Abort failed merge

git rebase --abort

Abort failed rebase

Stashing

6 commands
git stash

Save working changes

git stash pop

Apply & remove stash

git stash apply

Apply, keep stash

git stash list

List all stashes

git stash drop

Delete latest stash

git stash -u

Stash including untracked

Viewing History

8 commands
git log --oneline

Compact log

git log --graph --oneline --all

Visual branch graph

git log -p file.txt

File change history

git diff

Unstaged changes

git diff --staged

Staged changes

git diff branch1..branch2

Compare branches

git show HASH

Show specific commit

git blame file.txt

Who changed each line

Undoing Things

7 commands
git checkout -- file.txt

Discard file changes

git restore file.txt

Discard (modern)

git reset HEAD file.txt

Unstage a file

git reset --soft HEAD~1

Undo commit, keep staged

git reset --hard HEAD~1

Undo commit, discard all

git revert HASH

Create undo commit

git reflog

Recovery log (find lost commits)

Cherry-Pick & Tags

5 commands
git cherry-pick HASH

Apply specific commit

git tag v1.0

Create lightweight tag

git tag -a v1.0 -m 'msg'

Create annotated tag

git push origin --tags

Push all tags

git tag -d v1.0

Delete local tag

Worktrees

4 commands
git worktree add ../feature-wt feature

Create worktree for branch

git worktree list

List all worktrees

git worktree remove ../feature-wt

Remove worktree

git worktree prune

Clean stale worktrees

Bisect (Find Bug Commits)

5 commands
git bisect start

Begin binary search

git bisect bad

Mark current as broken

git bisect good HASH

Mark known good commit

git bisect run ./test.sh

Auto-bisect with script

git bisect reset

End bisect session

Submodules

5 commands
git submodule add URL path

Add submodule

git submodule update --init --recursive

Clone submodules

git submodule foreach git pull

Update all submodules

git submodule status

Show submodule commits

git rm --cached path && rm -rf path

Remove submodule

Hooks & Automation

6 commands
.git/hooks/pre-commit

Runs before each commit

.git/hooks/pre-push

Runs before each push

.git/hooks/commit-msg

Validate commit message

chmod +x .git/hooks/pre-commit

Make hook executable

npx husky install

Husky: managed git hooks

npx lint-staged

Run linters on staged files

Advanced Recovery

7 commands
git reflog

Full history of HEAD movements

git fsck --lost-found

Find dangling commits

git stash drop stash@{0}

Delete specific stash

git checkout HASH -- file.txt

Restore file from commit

git reset --hard ORIG_HEAD

Undo last reset/merge

git clean -fd

Remove untracked files+dirs

git gc --prune=now

Garbage collect objects

Interactive & Patch Mode

6 commands
git add -p

Stage hunks interactively

git checkout -p

Discard hunks interactively

git stash -p

Stash specific hunks

git rebase -i HEAD~5

Squash/reorder last 5 commits

git commit --fixup=HASH

Mark as fixup for rebase

git rebase -i --autosquash

Auto-apply fixup commits