Skip to main content

Git Advanced Topics

Take your Git skills to the next level with rebase, cherry-pick, stash, and understanding Git internals.

Git Rebase

Rebase rewrites history by replaying commits on top of another branch.

Rebase vs Merge

# Merge creates a merge commit
git checkout feature
git merge main

# Rebase replays feature commits on top of main
git checkout feature
git rebase main
When to use:
  • ✅ Rebase: Clean linear history, feature branches
  • ✅ Merge: Preserve complete history, shared branches
Never rebase public branches! It rewrites history and causes problems for collaborators.

Interactive Rebase

# Rebase last 3 commits
git rebase -i HEAD~3

# Rebase onto main
git rebase -i main
Interactive commands:
  • pick - Use commit as-is
  • reword - Change commit message
  • edit - Amend commit
  • squash - Combine with previous commit
  • fixup - Like squash but discard message
  • drop - Remove commit

Practical Example

# Clean up commits before PR
git rebase -i HEAD~5

# In editor:
pick abc123 Add feature
squash def456 Fix typo
squash 789abc Fix another typo
reword 123def Update docs

# Result: 2 clean commits instead of 5

Cherry-Pick

Apply specific commits from one branch to another.
# Apply commit abc123 to current branch
git cherry-pick abc123

# Cherry-pick multiple commits
git cherry-pick abc123 def456

# Cherry-pick without committing
git cherry-pick -n abc123
Use cases:
  • Apply hotfix to multiple branches
  • Port feature to different version
  • Recover specific changes

Git Stash

Temporarily save uncommitted changes.
# Stash changes
git stash

# Stash with message
git stash save "WIP: working on feature"

# List stashes
git stash list

# Apply latest stash
git stash apply

# Apply and remove stash
git stash pop

# Apply specific stash
git stash apply stash@{2}

# Drop stash
git stash drop stash@{0}

# Clear all stashes
git stash clear

Stash Options

# Stash including untracked files
git stash -u

# Stash including ignored files
git stash -a

# Create branch from stash
git stash branch new-branch stash@{0}

Git Reflog

Reflog records all HEAD movements - your safety net!
# View reflog
git reflog

# Reflog for specific branch
git reflog show feature-branch

# Recover lost commits
git reflog
# Find commit hash
git checkout abc123
git branch recovered-branch
Common scenarios:
  • Undo bad rebase
  • Recover deleted branch
  • Find lost commits
# Oops, deleted branch!
git branch -D important-feature

# Find it in reflog
git reflog
# See: abc123 HEAD@{5}: commit: important work

# Recover
git checkout abc123
git branch important-feature

Git Bisect

Binary search to find which commit introduced a bug.
# Start bisect
git bisect start

# Mark current as bad
git bisect bad

# Mark known good commit
git bisect good abc123

# Git checks out middle commit
# Test it, then mark:
git bisect good  # or
git bisect bad

# Repeat until found
# Git will tell you the first bad commit

# End bisect
git bisect reset

Automated Bisect

# Run test script automatically
git bisect start HEAD abc123
git bisect run npm test

Git Worktree

Work on multiple branches simultaneously.
# Create worktree
git worktree add ../feature-2 feature-branch

# List worktrees
git worktree list

# Remove worktree
git worktree remove ../feature-2
Use case: Review PR while continuing work on another branch.

Git Internals

Understanding how Git works under the hood.

Objects

Git stores everything as objects:
  1. Blob: File contents
  2. Tree: Directory structure
  3. Commit: Snapshot + metadata
  4. Tag: Named reference
# View object
git cat-file -p abc123

# View object type
git cat-file -t abc123

References

# .git/refs/heads/ - branches
# .git/refs/tags/ - tags
# .git/refs/remotes/ - remote branches

# View reference
cat .git/refs/heads/main

The Index (Staging Area)

# View index
git ls-files --stage

Advanced Workflows

Rebase Workflow

# Keep feature branch updated
git checkout feature
git fetch origin
git rebase origin/main

# Resolve conflicts if any
git add .
git rebase --continue

# Force push (safe)
git push --force-with-lease origin feature

Fixup Commits

# Make changes
git add .
git commit --fixup abc123

# Auto-squash during rebase
git rebase -i --autosquash main

Git Hooks

Automate tasks with Git hooks.
# Hooks location
ls .git/hooks/

# Common hooks:
# pre-commit - Run before commit
# pre-push - Run before push
# post-merge - Run after merge
Example pre-commit hook:
#!/bin/sh
# .git/hooks/pre-commit

npm test
if [ $? -ne 0 ]; then
  echo "Tests failed, commit aborted"
  exit 1
fi

Git Aliases

Speed up your workflow.
# Create aliases
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status

# Complex aliases
git config --global alias.lg "log --oneline --graph --all"
git config --global alias.unstage "reset HEAD --"

# Use them
git co main
git lg

Troubleshooting

Undo Last Commit (Keep Changes)

git reset --soft HEAD~1

Undo Last Commit (Discard Changes)

git reset --hard HEAD~1

Recover After Hard Reset

git reflog
git reset --hard abc123

Fix Commit on Wrong Branch

# On wrong-branch
git reset --hard HEAD~1

# Switch to correct branch
git checkout correct-branch
git cherry-pick abc123

Remove File from All History

# Use filter-branch (old way)
git filter-branch --tree-filter 'rm -f passwords.txt' HEAD

# Use filter-repo (recommended)
git filter-repo --path passwords.txt --invert-paths

Best Practices

Keep feature branches updated with main via rebase for clean history
Only rebase branches that haven’t been pushed or shared
Stash instead of committing half-done work
Reflog is your safety net - almost nothing is truly lost

Key Takeaways

  • Rebase: Clean linear history, but never on public branches
  • Cherry-pick: Apply specific commits across branches
  • Stash: Temporary storage for uncommitted work
  • Reflog: Safety net for recovering lost work
  • Bisect: Binary search for bug-introducing commits
  • Internals: Understanding objects makes Git less magical

🎉 Congratulations! You’ve completed the Git Crash Course. You now have the skills to use Git professionally, from basics to advanced workflows. Next: Linux Crash Course →