Forem

Thesius Code
Thesius Code

Posted on • Originally published at datanest-stores.pages.dev

Git Commands & Workflows Guide

Git Commands & Workflows Guide

Stop second-guessing your Git commands. This visual guide covers every branching strategy in use today — GitFlow, GitHub Flow, trunk-based development — with decision trees for merge vs rebase, interactive rebase recipes, and the "oh no" recovery commands that save your commit history. Includes CI/CD integration patterns showing how each workflow connects to automated pipelines.

What's Included

  • Branching Strategies — GitFlow, GitHub Flow, Trunk-Based Development with visual diagrams
  • Merge vs Rebase — Decision tree, conflict resolution, when to use each
  • Advanced Git Commands — Cherry-pick, stash, reflog, bisect, worktree
  • Interactive Rebase Recipes — Squash, reorder, edit, split commits
  • Recovery & Undo Guide — Undo commits, recover deleted branches, fix detached HEAD
  • CI/CD Integration Patterns — Branch protection, automated testing triggers, release automation
  • Git Hooks Reference — Pre-commit, pre-push, commit-msg hooks with examples
  • .gitconfig Optimization — Aliases, diff tools, merge strategies, global ignores

Preview / Sample Content

Merge vs Rebase — Decision Tree

Should I merge or rebase?
│
├── Is this a shared/public branch (main, develop)?
│   └── YES → Always MERGE (never rewrite shared history)
│
├── Is this my local feature branch?
│   ├── Want clean linear history? → REBASE onto main, then merge
│   └── Want to preserve branch topology? → MERGE
│
├── Are there merge conflicts?
│   ├── Simple conflicts → Rebase is fine
│   └── Complex multi-commit conflicts → Merge is safer
│
└── Team convention?
    ├── "Squash and merge" PRs → Rebase locally, squash on merge
    └── "Merge commit" PRs → Merge to preserve full history
Enter fullscreen mode Exit fullscreen mode

Essential Git Commands You Keep Forgetting

# Undo last commit but keep changes staged
git reset --soft HEAD~1

# Undo last commit and unstage changes
git reset --mixed HEAD~1

# Completely discard last commit and changes (DANGEROUS)
git reset --hard HEAD~1

# Amend last commit (message or add forgotten files)
git add forgotten-file.py
git commit --amend --no-edit

# Stash with a descriptive name
git stash push -m "WIP: refactoring auth module"

# Apply specific stash without removing it
git stash apply stash@{2}

# Cherry-pick a commit from another branch
git cherry-pick abc1234

# Cherry-pick without committing (stage changes only)
git cherry-pick --no-commit abc1234

# Find which commit introduced a bug (binary search)
git bisect start
git bisect bad HEAD
git bisect good v1.0.0
# Git checks out middle commit — test it, then:
git bisect good   # or git bisect bad
# Repeat until the guilty commit is found
git bisect reset

# Recover a deleted branch (within ~30 days)
git reflog
git checkout -b recovered-branch abc1234

# See what changed between two branches
git log main..feature --oneline
git diff main...feature --stat
Enter fullscreen mode Exit fullscreen mode

Interactive Rebase Recipes

# Squash last 4 commits into one
git rebase -i HEAD~4
# In editor, change 'pick' to 'squash' (or 's') for commits 2-4:
# pick   abc1234 Add user model
# squash def5678 Fix typo in user model
# squash 111aaaa Add validation
# squash 222bbbb Update tests

# Reorder commits
git rebase -i HEAD~3
# Simply rearrange the lines in the editor

# Edit a commit message 5 commits back
git rebase -i HEAD~5
# Change 'pick' to 'reword' (or 'r') on the target commit

# Split a commit into two
git rebase -i HEAD~3
# Change 'pick' to 'edit' on the target commit
git reset HEAD~1
git add file1.py && git commit -m "Part 1: add model"
git add file2.py && git commit -m "Part 2: add tests"
git rebase --continue
Enter fullscreen mode Exit fullscreen mode

Git Hooks — Pre-commit Example

#!/bin/sh
# .git/hooks/pre-commit — runs before every commit

# Prevent committing to main directly
branch=$(git rev-parse --abbrev-ref HEAD)
if [ "$branch" = "main" ]; then
    echo "ERROR: Direct commits to main are not allowed."
    echo "Create a feature branch: git checkout -b feature/your-feature"
    exit 1
fi

# Check for debug statements
if git diff --cached --name-only | xargs grep -l "debugger\|console\.log\|import pdb" 2>/dev/null; then
    echo "ERROR: Debug statements found in staged files."
    exit 1
fi

# Run linter on staged Python files
STAGED_PY=$(git diff --cached --name-only --diff-filter=ACM | grep '\.py$')
if [ -n "$STAGED_PY" ]; then
    python -m ruff check $STAGED_PY || exit 1
fi

exit 0
Enter fullscreen mode Exit fullscreen mode

Quick Reference Table

Command What It Does Safe on Shared Branch?
git merge feature Merge with merge commit ✅ Yes
git rebase main Replay commits on top of main ❌ Local only
git reset --soft HEAD~1 Undo commit, keep staged ❌ Local only
git reset --hard HEAD~1 Undo commit, discard all ❌ DANGEROUS
git revert abc123 Create inverse commit ✅ Yes
git cherry-pick abc123 Copy commit to current branch ✅ Yes
git stash Temporarily shelve changes ✅ Local operation
git reflog View all HEAD movements ✅ Read-only
git bisect Binary search for bad commit ✅ Read-only
git worktree add Multiple working directories ✅ Yes

Comparison: Branching Strategies

Aspect GitFlow GitHub Flow Trunk-Based
Branches main, develop, feature, release, hotfix main + feature main + short-lived feature
Complexity High Low Low
Release Style Scheduled releases Continuous deploy Continuous deploy
Best For Versioned software, mobile apps SaaS, web apps High-velocity teams
Branch Lifetime Days to weeks Hours to days Hours (< 1 day ideal)
CI/CD Fit Moderate (complex pipelines) Excellent Excellent
Team Size Any Small-medium Any (with feature flags)

Usage Tips

  1. Pick a branching strategy page that matches your team and stick with it — consistency matters more than which one you choose.
  2. Memorize the recovery commandsgit reflog has saved more developers than any other command.
  3. Set up the git hooks from the examples — they catch mistakes before they become PR review comments.
  4. Copy the .gitconfig aliases — the pack includes 30+ aliases that cut your typing in half.
  5. Use the merge vs rebase decision tree until it becomes instinct — it prevents the most common team workflow arguments.

This is 1 of 11 resources in the Cheatsheet Reference Pro toolkit. Get the complete [Git Commands & Workflows Guide] with all files, templates, and documentation for $12.

Get the Full Kit →

Or grab the entire Cheatsheet Reference Pro bundle (11 products) for $79 — save 30%.

Get the Complete Bundle →


Related Articles

Top comments (0)