Stax

Git Cheat Sheet

Practical Git command reference: branch, commit, rebase, merge, stash, cherry-pick, undo recipes, and the 10 commands that solve 95% of problems.

Daily commands

CommandWhat it does
git statusShow modified/staged/untracked files
git add <file> / git add .Stage changes for the next commit
git commit -m "msg"Create a commit from staged changes
git commit -am "msg"Stage all tracked + commit (skips untracked)
git pushSend commits to remote
git pullFetch + merge from remote
git pull --rebaseFetch + rebase local commits on top — cleaner history
git fetchUpdate remote-tracking refs without merging
git log --oneline -20Last 20 commits, one line each
git diffUnstaged changes
git diff --stagedStaged changes (about to be committed)

Branching

CommandWhat it does
git branchList local branches
git branch -aInclude remote branches
git switch <branch>Switch to existing branch (modern alias for checkout)
git switch -c new-branchCreate + switch to new branch
git checkout -b new-branchOlder syntax for the same
git branch -d <branch>Delete local branch (safe — refuses if unmerged)
git branch -D <branch>Force-delete (destructive)
git push -u origin <branch>Push + set upstream (first time)
git push origin --delete <branch>Delete remote branch

Merging & rebasing

# Merge another branch into yours (creates merge commit)
git merge feature-branch

# Rebase your current branch on top of main
git rebase main

# Interactive rebase — squash, reorder, edit last 5 commits
git rebase -i HEAD~5

# Continue after resolving conflicts
git add <resolved-files>
git rebase --continue
# Or abort:
git rebase --abort

Golden rule: never rebase commits that have been pushed to a shared branch. Rebase rewrites history; if others have pulled those commits, you'll fork their work.

Undo recipes (the most-searched section)

SituationFix
Unstage a filegit restore --staged <file> (or older git reset HEAD <file>)
Discard unstaged changes to a filegit restore <file> (or older git checkout -- <file>)
Discard ALL unstaged changesgit restore .
Amend last commit messagegit commit --amend
Add a forgotten file to last commitgit add file then git commit --amend --no-edit
Undo last commit, keep changes stagedgit reset --soft HEAD~1
Undo last commit, keep changes unstagedgit reset HEAD~1
Undo last commit, throw away changesgit reset --hard HEAD~1 ⚠️
Undo a pushed commit (safely)git revert <hash>
Recover a deleted commit/branchgit reflog → find hash → git checkout <hash>
I force-pushed and lost workgit reflog on remote (if available) or local backup

Stash

git stash                    # Save changes, clean working dir
git stash -u                 # Include untracked files
git stash push -m "msg"      # Stash with a message
git stash list               # See all stashes
git stash pop                # Apply latest + remove from list
git stash apply stash@{2}    # Apply specific stash, keep it
git stash drop stash@{0}     # Delete a stash
git stash branch new-branch  # Create branch from stash

Cherry-pick & advanced

# Apply a specific commit from another branch
git cherry-pick <hash>

# Pick multiple commits
git cherry-pick <hash1> <hash2>

# Pick a range (exclusive..inclusive)
git cherry-pick <start>^..<end>

# Find which branches contain a commit
git branch --contains <hash>

# Show what changed in a single commit
git show <hash>

# Search for a string across history
git log -S "deleted-function-name" --all

# Find when a line was added
git log -L 10,20:src/file.ts

Inspecting history

CommandWhat it shows
git log --oneline --graph --allASCII art of all branches
git log --since="2 weeks ago"Time-bounded history
git log --author="alice"Filter by author
git log --grep="fix"Filter by commit message
git log -- <file>History affecting one file
git blame <file>Per-line author + commit
git shortlog -snCommit count per author
git diff main..featureWhat feature has that main doesn't
git diff main...featureSame, from common ancestor (often what you want for PR review)

Tags & releases

git tag                          # List tags
git tag -a v1.0.0 -m "Release"   # Annotated tag (preferred)
git tag v1.0.0                   # Lightweight tag
git push origin v1.0.0           # Push single tag
git push origin --tags           # Push all tags
git tag -d v1.0.0                # Delete local
git push origin --delete v1.0.0  # Delete remote

Configuration shortcuts

# Identity
git config --global user.name "Your Name"
git config --global user.email "you@example.com"

# Default branch name
git config --global init.defaultBranch main

# Push only current branch by default
git config --global push.default current

# Useful aliases
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.st status
git config --global alias.lg "log --oneline --graph --all"
git config --global alias.unstage "restore --staged"

Git's mental model in one paragraph

Git tracks snapshots, not diffs. Three areas: working directory (your files), staging area / index (what's queued for the next commit), and repository (committed history). git add moves files from working → staging. git commit takes the staging area and creates a new snapshot. Branches are just movable pointers to commits. Once you internalise this — that everything is pointers to immutable snapshots — Git's commands stop feeling arbitrary.

The 10 commands that solve 95% of problems

status, add, commit, push, pull, checkout (or switch), branch, log, diff, stash. Memorise these and you can survive most workflows. The other 80 commands are for power-user moments that come up monthly, not daily.

คำถามที่พบบ่อย

What's the difference between git merge and git rebase?
Merge preserves the actual history — you get a merge commit showing where two branches converged. Rebase rewrites history — your commits are replayed on top of the target branch as if they were always linear. Practical rule: merge for shared/public branches (preserves what really happened, no force-push needed); rebase for your own feature branch before opening a PR (cleaner linear history). Never rebase a branch others have pulled — you'll force them into a merge nightmare.
How do I undo my last commit?
Three options based on intent. (1) Keep the changes, just unmake the commit: git reset --soft HEAD~1. (2) Keep changes but unstage: git reset HEAD~1 (default --mixed). (3) Throw away changes entirely: git reset --hard HEAD~1 — destructive, can't undo. (4) If already pushed: git revert HEAD creates a new commit that undoes the previous one — safe for shared branches. Never --hard reset a pushed branch unless you really know what you're doing.
I committed to the wrong branch — how do I fix it?
If not pushed yet: (1) git log to find the commit hash; (2) git checkout correct-branch; (3) git cherry-pick <hash>; (4) git checkout wrong-branch; (5) git reset --hard HEAD~1. If pushed: cherry-pick to correct branch, then git revert on the wrong branch. Always verify which branch you're on before committing — modern shell prompts show this.
What does git stash actually do?
git stash takes your current uncommitted changes (both staged and unstaged), saves them as a temporary 'stash', and resets your working directory to clean. You can then switch branches, pull, etc. Recover with git stash pop (apply + remove from stash list) or git stash apply (apply, keep in list). Use git stash list to see all stashes. Stashes are local — they don't get pushed. Don't use stash as a long-term storage; commit instead.
How do I see who changed a specific line?
git blame <file> shows commit, author, and date for every line. git log -p <file> shows full diffs of every change to that file. git log -L 10,20:<file> shows the history of just lines 10–20. For deleted code, git log -S 'deleted-string' finds when a string was removed. For 'why is this here' archaeology, blame is your friend — but don't use it as actual blame; it's for understanding context.

เครื่องมือที่เกี่ยวข้อง