DevOps & IaC15 min
Git — Workflows, Branching & Recovery Cheat Sheet
Complete Git reference — init, staging, commits, branching, merging, rebasing, stash, cherry-pick, revert, and common workflows for teams.
Setup & Config
git config --global user.name "Alice Smith"
git config --global user.email "alice@example.com"
git config --global core.editor "code --wait" # VS Code
git config --global init.defaultBranch main
git config --global pull.rebase false # default: merge on pull
git config --list # show all config
git config --global alias.st status # create alias
git config --global alias.lg "log --oneline --graph --all"
Init & Clone
git init # Init new repo
git init --bare my-repo.git # Bare repo (server-side)
git clone https://github.com/user/repo.git
git clone git@github.com:user/repo.git
git clone https://github.com/user/repo.git my-dir # clone to specific dir
git clone --depth 1 https://github.com/user/repo # shallow clone (no history)
git clone --branch develop https://github.com/user/repo # specific branch
Status, Staging & Committing
git status
git status -s # short / compact
# Staging
git add file.txt # Stage specific file
git add src/ # Stage directory
git add -p # Interactive patch staging (hunk by hunk)
git add -A # Stage all (new + modified + deleted)
git add . # Stage all in current dir
git rm --cached file.txt # Unstage (keep in working dir)
git restore --staged file.txt # Unstage (modern)
# Committing
git commit -m "feat: add login page"
git commit -am "fix: typo in readme" # stage all modified + commit
git commit --amend --no-edit # Add staged changes to last commit
git commit --amend -m "new message" # Rewrite last commit message
# Conventional commits (industry standard)
# feat: new feature
# fix: bug fix
# docs: documentation
# chore: build / tooling
# refactor: code restructure
# test: tests
Viewing History
git log
git log --oneline
git log --oneline --graph --all
git log --oneline -n 10 # last 10 commits
git log --since="2 weeks ago"
git log --author="Alice"
git log -- path/to/file # history of a file
git log --stat # files changed per commit
git log -S "search_string" # commits that changed a string (pickaxe)
git log --diff-filter=D -- '*.py' # commits that deleted Python files
git show abc1234 # show a specific commit
git show HEAD~2 # 2 commits ago
Diff
git diff # Unstaged changes
git diff --staged # Staged changes (vs last commit)
git diff HEAD # All changes vs last commit
git diff main..feature # Between branches
git diff abc1234 def5678 # Between commits
git diff --stat # Summary: files + insertions/deletions
git difftool # Open visual diff tool
Branching
git branch # List local branches
git branch -a # Local + remote branches
git branch -v # With last commit
git branch feature/login # Create branch
git switch feature/login # Switch to branch (modern)
git switch -c feature/login # Create + switch
git checkout -b feature/login # Create + switch (legacy)
git branch -d feature/login # Delete (safe, merged only)
git branch -D feature/login # Force delete
git branch -m old-name new-name # Rename
# Tracking remote branches
git branch --set-upstream-to=origin/main main
git branch -u origin/feature remote-feature
Remote
git remote -v # List remotes
git remote add origin git@github.com:user/repo.git
git remote add upstream https://github.com/original/repo.git
git remote rename origin upstream
git remote remove origin
git remote set-url origin git@github.com:user/new-repo.git
# Fetch / pull / push
git fetch origin # Download but don't merge
git fetch --all # Fetch all remotes
git pull # Fetch + merge
git pull --rebase # Fetch + rebase (cleaner history)
git push origin main
git push -u origin feature/login # Push + set upstream
git push --force-with-lease # Safer force push (checks for others' changes)
git push origin --delete feature/old # Delete remote branch
Merge
git merge feature/login # Merge into current branch
git merge --no-ff feature/login # Always create merge commit
git merge --squash feature/login # Squash all commits into one staged change
git merge --abort # Abort merge in progress
git mergetool # Open merge tool for conflicts
Rebase
git rebase main # Rebase current branch onto main
git rebase --onto main base feature # Transplant feature onto main, starting after base
git rebase -i HEAD~3 # Interactive rebase: last 3 commits
# In interactive rebase:
# pick = keep commit as-is
# reword= keep but edit message
# edit = stop to amend
# squash= combine with previous
# fixup = combine, discard message
# drop = delete commit
git rebase --continue # After resolving conflicts
git rebase --skip # Skip current conflicting commit
git rebase --abort # Abort rebase
Stash
git stash # Stash all uncommitted changes
git stash push -m "work in progress" # With message
git stash push -u # Include untracked files
git stash list # Show stash stack
git stash pop # Apply latest + remove from stash
git stash apply stash@{2} # Apply specific stash, keep in stack
git stash drop stash@{0} # Delete specific stash
git stash clear # Delete all stashes
git stash branch feature/from-stash # Create branch from stash
Cherry-pick
git cherry-pick abc1234 # Apply a specific commit to current branch
git cherry-pick abc1234..def5678 # Apply range of commits (excluding first)
git cherry-pick abc1234^..def5678 # Apply range (including first)
git cherry-pick --no-commit abc1234 # Apply changes but don't commit
git cherry-pick --abort
Undo & Recovery
# 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 entirely)
git reset --hard HEAD~1 # WARNING: data loss!
# Revert a commit (safe, creates new commit)
git revert abc1234
git revert HEAD # Revert last commit
# Restore file to last commit state
git restore file.txt # Discard working dir changes
git restore --staged file.txt # Unstage
# Find lost commits (reflog is your safety net!)
git reflog
git reset --hard HEAD@{2} # Go back to state 2 entries ago
git switch -c recovered HEAD@{3} # Recover into a new branch
Tags
git tag # List tags
git tag v1.0.0 # Lightweight tag
git tag -a v1.0.0 -m "Release v1.0.0" # Annotated tag
git tag -a v1.0.0 abc1234 # Tag specific commit
git push origin v1.0.0 # Push a tag
git push origin --tags # Push all tags
git tag -d v1.0.0 # Delete local tag
git push origin --delete v1.0.0 # Delete remote tag
Useful Patterns
# See what changed between main and current branch
git diff main...HEAD --stat
# Find the commit that introduced a bug (binary search)
git bisect start
git bisect bad # current commit is bad
git bisect good v1.0.0 # last known good state
# git bisect will checkout commits; mark as good/bad until found
git bisect reset
# Clean untracked files (dry run first!)
git clean -n # preview what would be deleted
git clean -fd # delete untracked files and dirs
# Grep across all files in repo
git grep "TODO" -- "*.py"
# Show who changed each line
git blame -L 10,20 file.txt
# Compact one-line log (great alias)
git log --oneline --graph --decorate --all
.gitignore Patterns
# Files
*.log
*.tmp
.DS_Store
Thumbs.db
# Directories
node_modules/
.venv/
__pycache__/
dist/
build/
.terraform/
# Environment / secrets
.env
.env.*
*.pem
*.key
credentials.json
