The complete practical Git reference — from daily workflow commands to advanced rebasing, cherry-picking, and recovering from mistakes.
Git is the one tool every developer uses every day. Yet most developers know only 5-10 commands and panic when something goes wrong. This is the reference I wish I'd had.
# Clone a repository
git clone https://github.com/user/repo.git
git clone --depth 1 https://github.com/user/repo.git # shallow clone (faster)
# See what changed
git status
git diff # unstaged changes
git diff --staged # staged changes
# Stage and commit
git add src/file.ts # stage specific file
git add src/ # stage directory
git add -p # interactive staging (patch mode)
git commit -m "feat: add login page"
git commit --amend # modify the last commit (before pushing!)
# Push / pull
git push origin main
git push -u origin feature/my-branch # set upstream on first push
git pull --rebase # pull + rebase instead of merge commit
# Create and switch
git checkout -b feature/my-feature # create + switch
git switch -c feature/my-feature # modern equivalent
# List branches
git branch -a # all local + remote
git branch -vv # with tracking and last commit
# Rename branch
git branch -m old-name new-name
# Delete branch
git branch -d feature/done # safe delete (merged only)
git branch -D feature/done # force delete
git push origin --delete feature/done # delete remote branch
# Update branch list from remote
git fetch --prune
# Clean log
git log --oneline --graph --decorate --all
# Who changed this line?
git blame src/file.ts
# Search commits
git log --grep="bug fix"
git log -S "functionName" # find when a string was added/removed
git log --since="2 weeks ago"
git log --author="Trong"
# Show a specific commit
git show abc1234
# Unstage a file (keep changes)
git restore --staged src/file.ts
# Discard changes in working directory
git restore src/file.ts
# Undo last commit (keep changes staged)
git reset --soft HEAD~1
# Undo last commit (keep changes unstaged)
git reset HEAD~1
# Undo last commit (discard changes) ⚠️
git reset --hard HEAD~1
# Revert a commit (safe for public history)
git revert abc1234
# I accidentally deleted a file — get it back
git restore src/deleted-file.ts
# Save work in progress
git stash
git stash push -m "WIP: login form"
git stash push --include-untracked
# List stashes
git stash list
# Apply and drop latest stash
git stash pop
# Apply a specific stash (keep it)
git stash apply stash@{2}
# Drop a stash
git stash drop stash@{1}
# Merge (creates a merge commit, preserves history)
git checkout main
git merge feature/my-feature
# Rebase (rewrites history, linear timeline)
git checkout feature/my-feature
git rebase main
# Interactive rebase — squash, reorder, reword commits
git rebase -i HEAD~3 # last 3 commits
git rebase -i main # all commits since branching
# Apply a specific commit to current branch
git cherry-pick abc1234
# Cherry-pick a range
git cherry-pick abc1234^..def5678
# Cherry-pick without committing (stage only)
git cherry-pick --no-commit abc1234
# Add to ~/.gitconfig
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.lg "log --oneline --graph --decorate --all"
git config --global alias.undo "reset --soft HEAD~1"
git config --global alias.cleanup "!git fetch -p && git branch -vv | grep gone | awk '{print \$1}' | xargs git branch -D"
# Find any commit (even deleted ones) — your lifesaver
git reflog
git checkout abc1234 # restore to that state
# I force-pushed and overwrote remote history
# Ask a teammate to push their local copy, or restore from reflog
# Recover a deleted branch
git reflog | grep 'branch-name'
git checkout -b branch-name abc1234
git reflog records every HEAD change for 90 days. Almost every git mistake is recoverable if you haven't run git gc (garbage collection).