🌐 Detecting your location…
📢 Advertisement — Configure AdSense in Appearance → Customize → AdSense Settings

Git Advanced Guide 2026: Rebase, Cherry-Pick, Bisect and Hooks

⏱️5 min read  ·  1,100 words

Git mastery separates junior from senior developers. Beyond basic add/commit/push, advanced Git features — interactive rebase, cherry-pick, bisect, worktrees, and hooks — dramatically improve your workflow and debugging capabilities. This guide covers the advanced Git features every developer should know in 2026.

Interactive Rebase — Rewrite History

# Squash last 5 commits into one
git rebase -i HEAD~5

# Opens editor with:
# pick a1b2c3d feat: add user authentication
# pick b2c3d4e fix: correct email validation
# pick c3d4e5f docs: add API documentation
# pick d4e5f6a test: add auth tests
# pick e5f6a7b chore: update dependencies

# Change to:
# pick a1b2c3d feat: add user authentication
# squash b2c3d4e fix: correct email validation
# squash c3d4e5f docs: add API documentation
# squash d4e5f6a test: add auth tests
# squash e5f6a7b chore: update dependencies

# Result: one clean commit with all changes

# Other rebase commands:
# reword - edit commit message
# edit   - pause to amend commit
# drop   - delete commit
# fixup  - squash but discard commit message

git cherry-pick — Apply Specific Commits

# Apply a commit from another branch
git cherry-pick a1b2c3d

# Apply a range of commits
git cherry-pick a1b2c3d..e5f6a7b

# Apply but don't commit yet (--no-commit)
git cherry-pick --no-commit a1b2c3d

# Cherry-pick a merge commit
git cherry-pick -m 1 merge_commit_hash

# Common use case: hotfix on main, apply to release branch
git checkout release/1.2
git cherry-pick fix/critical-security-patch

git bisect — Find the Bug-Introducing Commit

# Binary search through commits to find bug introduction
git bisect start
git bisect bad HEAD              # current commit has the bug
git bisect good v2.1.0           # this version was fine

# Git checks out a middle commit...
# Test if bug exists, then:
git bisect good    # no bug → search upper half
# or
git bisect bad     # bug exists → search lower half

# After finding the bad commit:
git bisect reset   # return to HEAD

# Automate bisect with a test script
git bisect start
git bisect bad HEAD
git bisect good v2.1.0
git bisect run python test_feature.py  # exits 0=good, 1=bad

# Git will tell you: "first bad commit is abc123..."
# Then examine: git show abc123

git worktree — Multiple Working Directories

# Work on two branches simultaneously without stashing
git worktree add ../myapp-hotfix hotfix/critical-bug

# Now you have:
# ./myapp/          — main working tree (current branch)
# ../myapp-hotfix/  — second working tree (hotfix branch)

# Both can run independently:
cd ../myapp-hotfix && python manage.py runserver 8001 &
cd myapp && python manage.py runserver 8000 &

# List worktrees
git worktree list

# Remove worktree when done
git worktree remove ../myapp-hotfix

git reflog — Recover Deleted Commits

# See every action Git has tracked (even resets and deleted commits)
git reflog

# Output:
# a1b2c3d HEAD@{0}: reset: moving to HEAD~1
# b2c3d4e HEAD@{1}: commit: add important feature  <-- this was "deleted"
# c3d4e5f HEAD@{2}: commit: initial setup

# Recover a "deleted" commit
git checkout -b recovery-branch HEAD@{1}

# Or reset to a previous state
git reset --hard HEAD@{1}

# Reflog is your safety net — all recent actions are tracked
# Default retention: 90 days for reachable refs, 30 for unreachable

git hooks — Automate Workflow

# Hooks live in .git/hooks/ (not committed to repo by default)
# Use pre-commit package or Husky for team-shared hooks

# Install pre-commit (Python)
pip install pre-commit

# .pre-commit-config.yaml
repos:
  - repo: https://github.com/astral-sh/ruff-pre-commit
    rev: v0.4.0
    hooks:
      - id: ruff          # linting
      - id: ruff-format   # formatting

  - repo: https://github.com/pre-commit/mirrors-mypy
    rev: v1.9.0
    hooks:
      - id: mypy

  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v4.6.0
    hooks:
      - id: trailing-whitespace
      - id: check-yaml
      - id: check-json
      - id: detect-private-key

# Install hooks
pre-commit install

# Run manually
pre-commit run --all-files

git stash Advanced Usage

# Stash with a descriptive name
git stash push -m "WIP: user authentication refactor"

# Stash specific files only
git stash push -m "database changes" -- src/db/models.py src/db/migrations/

# List stashes
git stash list
# stash@{0}: WIP: user authentication refactor
# stash@{1}: database changes

# Apply specific stash (doesn't remove it)
git stash apply stash@{1}

# Pop specific stash (applies and removes)
git stash pop stash@{0}

# Show stash diff
git stash show -p stash@{0}

# Apply stash to different branch
git checkout feature-branch
git stash apply stash@{2}

Useful Advanced Commands

# git log — powerful filtering
git log --oneline --graph --all    # visual branch history
git log --author="Alice" --since="1 week ago"
git log -S "functionName"          # find when a string was added/removed
git log -p -- path/to/file         # show diffs for a specific file

# git blame — who wrote each line
git blame src/auth.py
git blame -L 100,150 src/auth.py   # specific line range

# git diff — compare anything
git diff HEAD~3 HEAD -- file.py    # changes in file over last 3 commits
git diff branch1...branch2         # changes in branch2 since branching from branch1

# git show — inspect a commit
git show a1b2c3d
git show a1b2c3d:src/auth.py       # show file at that commit

# git grep — search across commits
git grep "TODO" HEAD -- "*.py"
git grep -n "functionName"         # with line numbers

# git add -p — stage hunks interactively
git add -p src/models.py           # choose which changes to stage

# Clean untracked files
git clean -n       # dry run
git clean -fd      # remove untracked files and directories

# git shortlog — contributor summary
git shortlog -sn --no-merges | head -10

Git Configuration Tips

# Useful global aliases
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.lg "log --oneline --graph --all --decorate"
git config --global alias.undo "reset HEAD~1 --mixed"
git config --global alias.wip "commit -am 'WIP'"

# Useful defaults
git config --global pull.rebase true     # rebase on pull (not merge)
git config --global push.default current # push current branch only
git config --global rerere.enabled true  # remember conflict resolutions
git config --global core.autocrlf input  # normalize line endings (macOS/Linux)

# Sign commits with GPG
git config --global commit.gpgsign true
git config --global user.signingkey <YOUR_GPG_KEY_ID>

Git mastery comes from using these features regularly, not just reading about them. Add interactive rebase to your PR workflow, use bisect when tracking bugs, and set up pre-commit hooks for consistency. The reflog is your safety net — almost nothing in Git is truly irreversible.

✍️ Leave a Comment

Your email address will not be published. Required fields are marked *

🌐 Read in:🇬🇧 English🇩🇪 Deutsch🇧🇷 Português🇸🇦 العربية🇮🇳 हिन्दी🇧🇩 বাংলা