The essential Git command library — from basic snapshots to advanced rewriting. Filter by category, copy commands instantly, and explore real-world examples.
git reset --soft HEAD~1git reflog + git reset --hard <hash>git branch --merged | grep -v main | xargs git branch -dgit init test-repo – all examples are harmless in a disposable repo.Understanding this flow is fundamental to mastering version control. The diagram reflects Git’s distributed nature and staging model, which enables flexible collaboration and safe experimentation.
Git is more than just commit and push. Modern teams rely on feature branching, interactive rebasing, and pull request workflows to maintain clean history. This cheat sheet integrates the most valuable commands used by open‑source maintainers and enterprise DevOps engineers.
Use git checkout -b feature/xyz (or git switch -c feature/xyz) to isolate development. Regularly integrate main with git rebase main to avoid merge clutter. Protected branches (main/develop) require PRs and status checks.
Never lose work: git reflog and git reset --soft HEAD~1 are lifesavers. For permanent history rewrites, use git revert on shared branches to maintain collaboration safety.
This guide references the official Git documentation, Pro Git Book (2nd Ed.), and internal DevOps runbooks from high‑performance engineering teams.
Situation: You committed too early and forgot to add a file. Solution: git reset --soft HEAD~1 → stage missing files → git commit -m "proper commit".
Backstory: Used by thousands of developers to fix messy histories without losing work.
git merge --abort is safe. Better: git mergetool or manually resolve conflicts, then git add . && git commit. For rebasing: git rebase --continue after fixes.
git branch --merged | grep -v "\*\|main" | xargs git branch -d deletes merged branches. Pair with git remote prune origin to remove stale remote tracking references.
git reflog to find the commit hash, then git checkout <hash> or git reset --hard <hash>. Reflog keeps local history of HEAD changes for 90 days.
git diff commit1 commit2 shows differences. For a visual tool: git difftool integrates with meld or vscode.
git init test-repo and create dummy files – the cheat sheet’s examples are harmless when tried in a disposable repo. Always verify destructive commands (git reset --hard, git clean -fd) by running git status first. You can also use git clone --local to copy a safe sandbox.