70 Mins

10. Advanced Git Commands

Advanced

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:

Bash
git log --oneline feature/big-feature

Apply only the specific commit to main. Git creates a NEW commit on main with the same changes, leaving the original untouched:

Bash
git checkout main
git cherry-pick abc1234

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:

Bash
git checkout feature/my-feature
git rebase main

If conflicts occur: resolve them, stage, and continue. To abort a rebase, use --abort.

Bash
git add .
git rebase --continue

# Or to abort:
git rebase --abort
Golden Rule of Rebasing: Never rebase branches that others are working on. Rebasing rewrites commit hashes — if someone else's branch is based on your old commits, their history will diverge catastrophically.

git log — Power User Mode

Compact one-line log:

Bash
git log --oneline

Visual branch graph:

Bash
git log --oneline --graph --all

Filter by author or by date range:

Bash
git log --author="Jane Smith"
git log --since="2025-01-01" --until="2025-01-31"

Show commits that changed a specific file, or search commit messages for a keyword:

Bash
git log -- src/auth.js
git log --grep="payment" --oneline

Show the actual changes (diff) for each commit:

Bash
git log -p

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):

Bash
git bisect start
git bisect bad

Mark a known-good commit (when it definitely worked):

Bash
git bisect good v1.0.0

Git checks out a commit in the middle. Test if the bug exists. Then tell Git if this commit works or has the bug:

Bash
git bisect good
git bisect bad

Git narrows down, checking out new midpoints each time. After it identifies the exact bad commit, end the bisect process:

Bash
git bisect reset

Knowledge Check

Ready to test your understanding of 10. Advanced Git Commands?

Back