5 Git Commands to Save Your Sanity

𝟱 𝗮𝗱𝘃𝗮𝗻𝗰𝗲𝗱 𝗚𝗶𝘁 𝗰𝗼𝗺𝗺𝗮𝗻𝗱𝘀 𝗜 𝘄𝗶𝘀𝗵 𝗜 𝗹𝗲𝗮𝗿𝗻𝗲𝗱 𝗲𝗮𝗿𝗹𝗶𝗲𝗿. Most engineers know add, commit, push. These 5 are the ones that actually save you when things go sideways. 𝟭. 𝗴𝗶𝘁 𝗿𝗲𝗳𝗹𝗼𝗴 — 𝘆𝗼𝘂𝗿 𝘀𝗮𝗳𝗲𝘁𝘆 𝗻𝗲𝘁 Accidentally did git reset --hard? Reflog keeps a record of every HEAD move for ~90 days. git reset --hard HEAD@{1} and you're back. 𝘕𝘰𝘵𝘩𝘪𝘯𝘨 𝘪𝘴 𝘵𝘳𝘶𝘭𝘺 𝘭𝘰𝘴𝘵. 𝟮. 𝗴𝗶𝘁 𝗿𝗲𝗯𝗮𝘀𝗲 -𝗶 — 𝗰𝗹𝗲𝗮𝗻 𝗵𝗶𝘀𝘁𝗼𝗿𝘆 𝗯𝗲𝗳𝗼𝗿𝗲 𝗮 𝗣𝗥 10 "WIP" commits, 3 typo fixes, 2 "oops" commits. Embarrassing. git rebase -i HEAD~10 — squash, reword, or drop commits before anyone reviews. 𝟯. 𝗴𝗶𝘁 𝗯𝗶𝘀𝗲𝗰𝘁 — 𝗯𝗶𝗻𝗮𝗿𝘆 𝘀𝗲𝗮𝗿𝗰𝗵 𝗳𝗼𝗿 𝗮 𝗯𝘂𝗴 500 commits. Something broke. No idea when. git bisect start → mark good and bad commits → Git finds the culprit in ~9 steps. Fully automated: git bisect run pytest tests/ 𝟰. 𝗴𝗶𝘁 𝘄𝗼𝗿𝗸𝘁𝗿𝗲𝗲 — 𝘁𝘄𝗼 𝗯𝗿𝗮𝗻𝗰𝗵𝗲𝘀, 𝗻𝗼 𝘀𝘁𝗮𝘀𝗵𝗶𝗻𝗴 git worktree add ../hotfix hotfix/auth Full second working copy of the same repo, different branch. No stash, no context-switch chaos. 𝟱. 𝗴𝗶𝘁 𝗰𝗵𝗲𝗿𝗿𝘆-𝗽𝗶𝗰𝗸 --𝗻𝗼-𝗰𝗼𝗺𝗺𝗶𝘁 Apply one commit from another branch — but inspect it before it lands. git cherry-pick --no-commit a3f9b2c → review with git diff --staged → commit when satisfied. 𝗴𝗶𝘁 𝗶𝘀 𝗻𝗼𝘁 𝗮 𝘀𝗮𝘃𝗲 𝗯𝘂𝘁𝘁𝗼𝗻. 𝗜𝘁'𝘀 𝗮 𝘁𝗶𝗺𝗲 𝗺𝗮𝗰𝗵𝗶𝗻𝗲. Learn the power commands and you'll never fear a bad commit again. #git #devtools #softwaredevelopment #versioncontrol #engineeringlife

To view or add a comment, sign in

Explore content categories