Advanced Git
Cherry-pick, bisect, reflog and blame. The advanced commands that recover lost work and find regressions fast, and which of them are worth learning first.
Why Learn Advanced Git?
Once you know commits, branches, merges, and remotes, the next step is learning the tools that save time during debugging, recovery, and automation. Advanced Git commands are not used every hour, but when you need them, they can be incredibly valuable.
In this lesson, you will meet six high-impact concepts:
cherry-pickbisectreflog- submodules
- hooks
- aliases
Tip: Advanced Git is less about memorizing every option and more about recognizing which tool fits the situation.
Cherry-Pick
git cherry-pick copies the change introduced by an existing commit and applies it to your current branch.
This is useful when you want one specific fix without merging a whole branch.
Example:
git switch release/1.2
git cherry-pick a1b2c3d
A common real-world use case is backporting a bug fix from main to an older release branch.
When to Use It
Use cherry-pick when:
- a single commit should move to another branch
- you want to avoid merging unrelated work
- you are backporting a hotfix
Be careful not to overuse it, because duplicated commits across branches can make history harder to follow.
Bisect
git bisect helps you find which commit introduced a bug by performing a binary search through history.
Start the process:
git bisect start
git bisect bad
git bisect good v1.0.0
Git checks out a midpoint commit. You test it and tell Git whether that commit is good or bad.
git bisect good
or:
git bisect bad
Git keeps narrowing the range until it identifies the likely offending commit.
Why It Matters
If a problem appeared somewhere in the last 100 commits, bisect can reduce manual hunting dramatically.
Reflog
git reflog records where HEAD and branch references have pointed over time. It is a lifesaver when you accidentally lose track of commits after reset, rebase, or checkout operations.
Run:
git reflog
You may see entries like:
a1b2c3d HEAD@{0}: reset: moving to HEAD~1
d4e5f6g HEAD@{1}: commit: Add deployment notes
If you hard-reset by mistake, reflog can help you recover:
git reset --hard d4e5f6g
Note: Reflog tracks your local reference movement. It is one of the best recovery tools for “I think I lost my commit” moments.
Submodules Intro
A submodule lets one Git repository include another repository at a specific commit.
Example use cases:
- sharing a library across projects
- embedding a documentation site repo inside a larger platform repo
- keeping a dependency as a separate repository with its own history
Add a submodule:
git submodule add https://github.com/example/shared-lib.git libs/shared-lib
Clone a project with submodules:
git clone --recurse-submodules https://github.com/example/platform.git
Submodules are powerful, but they add complexity because the parent repo tracks a pointer to a specific child repo commit rather than copying its full contents into normal history.
Hooks Intro
Git hooks are scripts that run automatically at certain lifecycle events. They live in .git/hooks for a repository.
Common examples:
pre-commit: run formatting or tests before a commit completescommit-msg: validate commit message formatpre-push: run checks before pushing
A simple pre-commit hook might run:
npm test
Hooks can improve quality by catching issues early, but local hooks are not always shared automatically unless your team uses a dedicated hook management approach.
Aliases
Aliases let you define short names for longer Git commands.
Create a useful alias for a compact log view:
git config --global alias.lg "log --oneline --graph --decorate --all"
Now run:
git lg
Another popular alias:
git config --global alias.st status
Then:
git st
Aliases reduce typing and make frequent commands easier to remember.
Putting Advanced Git to Work
Here is a practical scenario:
- a bug appears in production
git bisecthelps find the bad commit- you use
git revertor a hotfix commit to fix it - you
cherry-pickthe fix into a release branch - a teammate accidentally resets a branch, and
git refloghelps recover the lost commit - a
pre-pushhook prevents future broken pushes
These tools are different, but together they make teams more resilient.
When to Reach for These Tools
Use cherry-pick when
- one commit should move across branches
Use bisect when
- you know something broke, but not which commit caused it
Use reflog when
- a commit seems lost after reset, rebase, or checkout
Use submodules when
- a separate repository must be referenced inside another repo
Use hooks when
- repetitive checks should run automatically
Use aliases when
- you want faster daily command usage
What You Should Remember
Advanced Git is about control and recovery. cherry-pick moves specific changes, bisect finds the commit that broke something, and reflog helps recover from history mistakes. Submodules, hooks, and aliases extend Git into larger workflows and better habits.
You do not need these commands every day, but learning them now makes you much more confident when the simple path stops being enough.
Test Your Understanding
Exercise 1: Backporting one fix
Scenario: A bug fix already exists on main, and you need the same single commit on release/1.2 without merging the whole branch.
Question: Which advanced Git tool from the tutorial is designed for that?
Exercise 1: Backporting one fix
Choose the command meant for applying one specific existing commit to another branch.
Exercise 2: Finding the bad commit quickly
Scenario: A regression appeared sometime in the last 100 commits, but nobody knows which one caused it.
Question: Which tool from the lesson is built to narrow that search efficiently?
Exercise 2: Finding the bad commit quickly
Pick the advanced Git workflow that uses a binary search through history.
Exercise 3: Recovering after a mistake
Scenario: You ran a hard reset and think you lost a useful commit from yesterday.
Question: Which advanced Git feature from the tutorial gives you the best chance to find and recover it?
Exercise 3: Recovering after a mistake
Choose the recovery tool that records where HEAD and branch references pointed locally.
Frequently Asked Questions
What does git cherry-pick do?
It applies the changes from one commit onto your current branch as a new commit with a new hash. The classic use is a hotfix: the fix is committed on main and you need the same change on a release branch without bringing everything else along. git cherry-pick <sha> does it; -x appends a line recording the original hash, which is worth using so the duplication is traceable. Cherry-picking the same change into two branches that later merge can produce a conflict, which is the main reason to prefer merging where you can.
How does git bisect find a bug?
By binary search through history. You tell it a commit where the bug exists (git bisect bad) and one where it does not (git bisect good), and it checks out the midpoint for you to test, narrowing by half each round, around ten steps for a thousand commits. Mark each as good or bad and it names the first bad commit. git bisect run ./test.sh automates it entirely when you have a script that exits non-zero on failure, which turns an afternoon of guessing into a couple of minutes.
What is git reflog and when does it save you?
The reflog records every position HEAD has held in your local repository, including states no branch points at any more. That makes it the recovery tool for a bad reset --hard, a rebase that went wrong, a deleted branch, or a commit you amended away: git reflog lists the history, then git reset --hard HEAD@{5} or git switch -c recovered <sha> brings it back. Entries last about 90 days by default. It is local only, which is why committing is the real safety net.
What is the difference between git cherry-pick and git rebase?
Rebase replays a whole series of commits onto a new base; cherry-pick applies one or a few chosen commits. Under the surface they do much the same work, rebase is essentially a scripted sequence of cherry-picks, so the choice is about scope. Rebase for "move my branch on top of the latest main", cherry-pick for "I need just that one fix over here".
How do I find which commit introduced a specific line?
git blame <file> annotates every line with the commit, author and date that last changed it, and git blame -L 40,60 <file> limits it to a range. When blame only shows a formatting commit, git blame -w --ignore-rev <sha> skips whitespace changes and a specified reformat. For searching by content rather than location, git log -S "functionName" finds commits where the number of occurrences of that string changed, which is the fastest way to locate when something was added or deleted.
Which advanced Git commands are actually worth learning first?
Three, in this order. git reflog, because it turns most disasters into inconveniences and takes five minutes to understand. git bisect, because nothing else finds a regression in a long history nearly as fast. And git log -S, because "when did this string appear" is a question you ask constantly. Cherry-pick and interactive rebase are valuable but situational, and submodules are best avoided until you have a specific need for them.