10. Advanced Git Commands
Advanced Git Commands
The commands in this module separate developers who use Git as a basic save-and-push tool from those who use it as a precision instrument for managing complex codebases. These are the commands that become second nature to senior engineers.
🍒 Cherry-Pick — Stealing a Single Commit
Cherry-pick applies a specific commit from any branch onto your current branch. Use it to pull a single bug fix from a feature branch without merging the whole branch. First, find the commit hash you want:
Apply only the specific commit to main. Git creates a NEW commit on main with the same changes, leaving the original untouched:
🔄 Rebase — Replaying Commits on a New Base
Rebase rewrites your branch's history by replaying your commits on top of the latest main. This keeps history linear and clean compared to merge commits.
Situation: main has new commits since you branched off. Your branch has 2 commits. Instead of a merge commit, replay your work on top of current main. Git will temporarily remove your commits, fast-forward to the latest main, and replay your commits on top:
If conflicts occur: resolve them, stage, and continue. To abort a rebase, use --abort.
📊 git log — Power User Mode
Compact one-line log:
Visual branch graph:
Filter by author or by date range:
Show commits that changed a specific file, or search commit messages for a keyword:
Show the actual changes (diff) for each commit:
🔍 git bisect — Finding the Broken Commit
git bisect does a binary search through history to find when a bug was introduced. Essential for "it worked last week, now it's broken". Start bisect and mark the current commit as bad (bug is present):
Mark a known-good commit (when it definitely worked):
Git checks out a commit in the middle. Test if the bug exists. Then tell Git if this commit works or has the bug:
Git narrows down, checking out new midpoints each time. After it identifies the exact bad commit, end the bisect process:
Knowledge Check
Ready to test your understanding of 10. Advanced Git Commands?