Advanced Git Commands
55 min`git cherry-pick` allows you to apply a specific commit from one branch to another, copying a commit's changes without merging the entire branch. Cherry-pick is useful when you need a specific fix or feature from another branch. It creates a new commit with the same changes but different commit hash. Understanding cherry-pick enables selective commit application. Cherry-pick is powerful for applying specific changes across branches.
`git bisect` helps you find which commit introduced a bug by performing a binary search through commit history. You mark commits as good or bad, and Git narrows down to the problematic commit. Bisect is invaluable for debugging when you know a bug exists but not when it was introduced. Understanding bisect enables efficient bug hunting. Bisect can save hours of manual searching.
`git reflog` shows a log of where your HEAD has been, useful for recovering from mistakes like accidental resets or deleted branches. Reflog records all HEAD movements, even for commits not referenced by branches. This makes reflog a safety net—you can almost always recover 'lost' commits. Understanding reflog enables recovery from many mistakes. Reflog is your Git safety net.
`git submodule` allows you to keep a Git repository as a subdirectory of another Git repository, enabling you to include external projects as dependencies. Submodules track specific commits of external repositories. They're useful for including libraries or shared code. Understanding submodules enables managing project dependencies. Submodules require careful management but are powerful for dependency management.
`git worktree` enables you to check out multiple branches simultaneously in different directories, allowing you to work on multiple branches without switching. Worktrees are useful when you need to compare branches, test different versions, or work on multiple features. Each worktree has its own working directory but shares the same repository. Understanding worktrees enables efficient multi-branch workflows. Worktrees are powerful for parallel development.
Best practices include using cherry-pick sparingly (prefer merging when possible), using bisect for bug hunting, checking reflog when you think you've lost work, understanding submodule complexity before using them, and using worktrees for parallel work. Understanding advanced commands enables powerful Git workflows. These commands are advanced but essential for complex scenarios. Advanced commands expand Git's capabilities significantly.
Key Concepts
- git cherry-pick applies specific commits from one branch to another.
- git bisect performs binary search to find bug-introducing commits.
- git reflog shows HEAD movement history for recovery.
- git submodule includes external repositories as dependencies.
- git worktree enables multiple branch checkouts simultaneously.
Learning Objectives
Master
- Using git cherry-pick to apply specific commits
- Using git bisect to find bug-introducing commits
- Using git reflog to recover from mistakes
- Understanding git submodule and worktree
Develop
- Understanding advanced Git capabilities
- Using Git tools for complex scenarios
- Recovering from Git mistakes effectively
Tips
- Use git reflog to recover from accidental resets or deletions.
- Use git bisect to efficiently find when bugs were introduced.
- Use git cherry-pick sparingly—prefer merging when possible.
- Understand submodule complexity before using them.
Common Pitfalls
- Overusing cherry-pick, creating duplicate commits.
- Not using reflog when you think you've lost work.
- Using submodules without understanding their complexity.
- Not understanding that bisect requires manual testing.
Summary
- Advanced Git commands enable powerful workflows.
- git cherry-pick applies specific commits across branches.
- git bisect helps find bug-introducing commits.
- git reflog enables recovery from mistakes.
- Understanding advanced commands expands Git capabilities.
Exercise
Use cherry-pick to apply a specific commit from one branch to another.
# You have a bug fix in a feature branch that you want in main
git checkout main
git cherry-pick abc1234
# Or cherry-pick multiple commits
git cherry-pick abc1234 def5678 ghi9012
# If there are conflicts, resolve them and continue
git add .
git cherry-pick --continue