WebPiki
tutorial

Advanced Git: rebase, stash, bisect, worktree

Go beyond add/commit/push. Practical guide to interactive rebase, stash, bisect, worktree, reflog, and other power-user Git features.

Git branch history tree

If your Git vocabulary is limited to add, commit, push, and pull, you're using about 10% of what Git offers. The other 90% contains features that can seriously speed up your workflow.

git rebase — Clean History

Merge and rebase both combine branches, but the results look different.

Merge preserves the fork-and-join structure. A merge commit appears, and the history shows branches diverging and converging.

Rebase takes your branch's commits and replays them on top of the target branch. The history becomes a straight line, as if you'd been working on the target branch all along.

# Rebase feature branch onto main
git checkout feature
git rebase main

When to Rebase

On personal feature branches, to pull in the latest changes from main. git pull --rebase fetches remote changes and rebases in one command.

# Pull with rebase instead of merge
git pull --rebase origin main

The Golden Rule

Never rebase commits that have been pushed and shared. Others might be building on those commits. Rebase changes commit hashes, which creates conflicts with anyone else's work. The common guideline: merge on shared branches, rebase on personal branches.

git rebase -i — Commit Surgery

Interactive rebase lets you squash, reorder, rename, and delete commits.

# Clean up the last 3 commits
git rebase -i HEAD~3

An editor opens with a commit list:

pick abc1234 Add feature A
pick def5678 Fix typo
pick ghi9012 Finish feature A

Change the command at the start of each line:

  • pick — Keep the commit
  • squash (or s) — Merge into previous commit
  • fixup (or f) — Merge into previous, discard message
  • reword (or r) — Edit commit message
  • drop (or d) — Delete commit
  • edit (or e) — Pause at this commit for changes
pick abc1234 Add feature A
fixup def5678 Fix typo
fixup ghi9012 Finish feature A

Three commits become one, keeping only the first message. Essential for cleaning up WIP commits before opening a PR.

git stash — Temporary Shelving

You're mid-change and need to switch branches urgently. The work isn't ready to commit. Stash saves your changes temporarily.

# Stash current changes
git stash

# Stash with a descriptive message
git stash push -m "login form work in progress"

# List stashes
git stash list

# Restore most recent stash
git stash pop

# Restore a specific stash
git stash pop stash@{2}

# Restore without removing from stash
git stash apply

pop restores and deletes from the stash. apply restores but keeps it. apply is useful when you need the same changes on multiple branches.

Partial Stash

# Stash specific files
git stash push -m "description" -- path/to/file.ts

# Include untracked files
git stash -u

git bisect — Binary Search for Bugs

"It worked two weeks ago but something broke and I don't know when." Bisect uses binary search across your commit history to find the exact commit that introduced a bug.

# Start bisect
git bisect start

# Mark current state as bad
git bisect bad

# Mark a known-good commit
git bisect good abc1234

Git checks out a commit halfway between good and bad. Test it and report:

# Bug present
git bisect bad

# Bug absent
git bisect good

Repeat, and Git narrows the range by half each time. Among 1,000 commits, you'll find the culprit in under 10 steps.

# When done
git bisect reset

Automate It

# Let a test script do the work
git bisect run npm test

If the test passes, it's marked good. If it fails, bad. No human input needed — Git finds the breaking commit on its own.

git worktree — Multiple Branches at Once

Working on a feature branch when a hotfix request comes in. Stash, switch branches, fix, switch back, pop stash... tedious.

Worktree checks out a different branch in a separate directory from the same repo.

# Check out main in a sibling directory
git worktree add ../project-main main

# Create and check out a new branch
git worktree add ../project-hotfix -b hotfix/urgent

# List worktrees
git worktree list

# Remove a worktree
git worktree remove ../project-hotfix

The .git data is shared, so disk usage stays low. Each directory builds and tests independently.

Better git log

The default git log output is verbose. A few options make it much more useful.

# Compact one-line view
git log --oneline

# Branch graph visualization
git log --oneline --graph --all

# History of a specific file
git log --oneline -- src/auth.ts

# Find commits where a string was added or removed
git log -S "deprecated" --oneline

# Date range
git log --after="2026-01-01" --before="2026-02-01"

# Specific author
git log --author="mason"

The -S flag (pickaxe) finds commits where a specific string appears in the diff. Great for answering "when was this function added?"

git reflog — Your Safety Net

Deleted a branch by mistake? Reset away commits you needed? Reflog is the last line of defense.

# See everywhere HEAD has pointed
git reflog

# Restore to a specific point
git reset --hard HEAD@{5}

Reflog is local only and retained for 90 days by default. It's the reason people say "it's almost impossible to permanently lose data in Git."

Practical Tips

Use conventional commit prefixes. feat:, fix:, refactor:, docs: — following Conventional Commits makes history scannable and enables automatic changelog generation.

Set up a global .gitignore. .DS_Store, Thumbs.db, IDE config files — these don't belong in project-level .gitignore. Handle them globally.

git config --global core.excludesfile ~/.gitignore_global

Create aliases. Shorten commands you use constantly.

git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.lg "log --oneline --graph --all"
git config --global alias.st "status --short"

Git is a tool. The better you know your tools, the more time you spend on actual code. You don't need to memorize everything here — just remember these features exist so when the right situation comes up, you know where to look.

#Git#version control#developer tools#CLI#collaboration

관련 글