Fixed a critical bug on ‘dev’ but not ready to merge the whole branch to ‘prod’? Use git cherry-pick [hash].
Category: Git
Git: Keep History Clean with Squash Rebase
Don’t let ‘fixed typo’ or ‘test’ commits ruin your main branch. Merge them into one clean commit before pushing. git rebase -i HEAD~3 # Pick first, squash others
Git: Use ‘Stash Pop’ to Move Work Between Branches
Started working on the wrong branch? Stash your changes, switch branch, and pop them back. git stash git checkout correct-branch git stash pop
Git: Use git clean to Remove Untracked Files Safely
Manual deletion of generated files is tedious. git clean removes all untracked files at once. ⚠️ Test First (Dry Run): git clean -n # Shows what WOULD be deleted without actually deleting Remove Untracked Files: git clean -f # Force remove files Remove Untracked Files AND Directories: git clean -fd # -d = directories Remove […]
Git: Use git revert Instead of git reset for Shared Branches
git reset rewrites history – dangerous on shared branches. git revert creates new commit that undoes changes safely. ❌ Wrong – git reset on Shared Branch: git reset –hard HEAD~1 # Removes commit from history git push –force # DANGEROUS – rewrites history for everyone! # Team members’ branches now out of sync ✅ Right […]
Git: Use .gitattributes to Handle Line Endings Across OS
Team uses Windows and Mac? Mixed line endings (CRLF/LF) cause constant unnecessary diffs. Fix with .gitattributes. Create .gitattributes in repo root: # Normalize all text files to LF in repo * text=auto # Force LF for these file types *.js text eol=lf *.ts text eol=lf *.css text eol=lf *.html text eol=lf *.json text eol=lf # […]
Git: Use git notes to Add Comments to Commits Without Changing History
Want to add context to old commits without rewriting history? Git notes attach metadata to any commit. Add Note to Commit: # Add note to last commit git notes add -m “Reviewed by John. Deployed to staging 2024-03-15” # Add note to specific commit git notes add -m “This fixed the prod outage on Black […]
Git: Use git sparse-checkout to Clone Only Specific Folders
Cloning huge repo when you only need one folder wastes time and disk. Sparse checkout downloads only what you need. Clone with Sparse Checkout: # Clone repository (no files yet) git clone –no-checkout https://github.com/user/repo.git cd repo # Enable sparse checkout git sparse-checkout init –cone # Specify folders you want git sparse-checkout set frontend/src backend/api # […]
Git: Use git switch and git restore Instead of Confusing git checkout
git checkout does too many things (switch branches, restore files, create branches). Git 2.23+ splits it into clear commands. Old Confusing Way: # Switch branch git checkout main # Create and switch to new branch git checkout -b feature # Restore file git checkout — file.txt # All same command! Confusing! New Clear Commands: # […]
Git: Use git log –graph to Visualize Branch History
Understanding branch history in text is hard. –graph flag shows visual tree. Pretty Graph: git log –graph –oneline –all # Output: # * abc1234 (HEAD -> main) Merge feature # |\ # | * def5678 (feature) Add feature # | * ghi9012 Feature progress # * | jkl3456 Fix bug # |/ # * mno7890 […]
Git: Use git reflog to Recover Lost Commits After Reset
Accidentally did git reset –hard and lost commits? Reflog keeps history of all HEAD movements. View Reflog: git reflog # Output: # abc1234 HEAD@{0}: reset: moving to HEAD~3 # def5678 HEAD@{1}: commit: Important feature ← Lost commit! # ghi9012 HEAD@{2}: commit: Bug fix Recover Lost Commit: # Option 1: Checkout specific commit git checkout def5678 […]
Git: Use git worktree to Work on Multiple Branches Simultaneously
Switching branches loses unsaved work. Worktree creates separate folder for each branch. Create Worktree: # Create worktree for feature branch git worktree add ../myproject-feature feature-branch # Now you have: # ~/myproject/ (main branch) # ~/myproject-feature/ (feature branch) Work in Both: Open both folders in separate VS Code windows – work on both branches at once! […]
Git: Use git cherry-pick to Copy Specific Commits Between Branches
Need one commit from another branch but not all changes? Cherry-pick copies specific commits. Pick Single Commit: # Find commit hash you want git log feature-branch –oneline # abc1234 Fix critical bug # Switch to target branch git checkout main # Cherry-pick that commit git cherry-pick abc1234 # Commit is now in main branch! Pick […]
Git: Use git blame -L to See Who Changed Specific Lines
Whole file blame is overwhelming. See only who changed specific lines you care about. Blame Specific Line Range: # See who changed lines 50-60 git blame -L 50,60 file.js # See who changed from line 100 to end git blame -L 100,+1000 file.js Ignore Whitespace Changes: # Ignore commits that only changed indentation git blame […]
Git: Use git commit –fixup to Easily Amend Older Commits
Found a typo in commit from 5 commits ago? –fixup marks it for automatic squashing later. Create Fixup Commit: # Find commit hash to fix git log –oneline # abc1234 Add user authentication # def5678 Add email validation ← Want to fix this # Make your fix, then: git add . git commit –fixup def5678 […]
Git: Use git restore to Unstage Files Without Losing Changes
Accidentally staged files you didn’t mean to commit? git restore unstages them without deleting changes. Unstage Specific File: # Staged config.json by mistake git add config.json # Unstage it (keeps changes in working directory) git restore –staged config.json Unstage All Files: git restore –staged . Discard Changes (Careful!): # Unstage AND discard changes git restore […]
Git: Use git bisect to Find Which Commit Introduced a Bug
Bug appeared somewhere in last 100 commits but don’t know where? Git bisect does binary search to find the exact commit. Start Bisect: # Mark current commit as bad git bisect start git bisect bad # Mark last known good commit (e.g., a week ago) git bisect good abc1234 Git Checks Out Middle Commit: # […]
Git: Stash Untracked Files with –include-untracked Flag
Regular git stash only saves tracked files. New files you created get left behind. Include Untracked Files: git stash –include-untracked # Or shorthand: git stash -u Now new files are stashed too! Apply Later: git stash pop Stash Everything (Including Ignored Files): git stash –all Useful before git clean or switching branches with incomplete work.
Git: Create Aliases for Common Commands to Save Typing
Tired of typing git status, git commit -am, git log –oneline? Create short aliases. Setup: git config –global alias.st status git config –global alias.co checkout git config –global alias.br branch git config –global alias.cm ‘commit -m’ git config –global alias.lg ‘log –oneline –graph –all’ Now Use: git st # Instead of: git status git co […]
Git Hooks Automation: Pre-commit Checks That Save Hours of Debugging
Tired of catching bugs in code review? Git hooks automatically validate code before it even reaches the repository. #!/bin/bash # .git/hooks/pre-commit # Exit immediately if any command fails set -e echo “🚀 Running pre-commit checks…” # 1. Check for debugging statements echo “🔍 Checking for console.log statements…” if git diff –cached –name-only | xargs grep […]
Git: Find and Remove Sensitive Data from Entire Repository History
Accidentally committed API keys or passwords? Deleting the file in a new commit doesn’t remove it from history. Here’s how to purge it completely. ⚠️ The Problem: # You committed config.json with API key git add config.json git commit -m “Add config” git push # Then realized mistake and deleted it git rm config.json git […]
Git: Undo Last Commit Without Losing Changes (3 Different Scenarios)
Committed too early or to wrong branch? Here’s how to undo commits without losing your work, depending on whether you’ve pushed or not. Scenario 1: Undo Last Commit (Not Pushed Yet) # Keep changes in working directory (most common) git reset –soft HEAD~1 # Now your changes are uncommitted, ready to re-commit # Use case: […]
Speed Up Git Operations 10x by Cleaning Up Bloated Repository History
Is ‘git clone’ taking 10 minutes? Your repository probably has bloated history from large files that were deleted years ago but still exist in Git’s object database. Diagnose the Problem: # Check repository size du -sh .git # If this is >500MB for a code project, you have bloat # Find largest objects in Git […]
Recover Deleted Git Commits Even After Hard Reset (It’s Not Gone)
Accidentally did ‘git reset –hard’ and lost hours of work? Your commits aren’t deleted – they’re just orphaned. Here’s how to find them. The Magic Command – Reflog: git reflog This shows every HEAD movement in your repository, even deleted commits. Output looks like: a1b2c3d HEAD@{0}: reset: moving to HEAD~3 e4f5g6h HEAD@{1}: commit: Added user […]
The Hidden Cost of Large Commits (It’s Not Code Review)
Big commits don’t just slow reviews. Real damage Breaks git bisect Makes rollback risky Hides causal relationships Rule One commit = one reason. Mental model If you can’t describe the commit in one sentence, it’s too big.
Why git pull Sometimes Corrupts Team History (Even Without Conflicts)
The issue is not conflicts.It’s implicit merge commits. What happens git pull = fetch + merge That merge commit: Breaks linear history Hides real change intent Makes bisect harder Safer default git config –global pull.rebase true Why this matters Rebase preserves intent, merge preserves state.Teams need intent.
Recover a Deleted Commit Without Panic
Commit gone? Not really. git reflog git checkout <hash> Why Git never deletes immediately — it just forgets references.
Why git pull Is Dangerous in Long-Lived Branches
git pull = fetch + merge. That merge may silently introduce conflicts. Safer git fetch git rebase origin/main Why Rebase keeps history linear and reviewable.
Stop Using git pull (Yes, Really)
git pull = fetch + mergeYou lose control. Better git fetch git rebase origin/main Why Cleaner history Fewer merge commits Easier rollback Rule Explicit is safer than automatic.























