Git Tutorial
- 1. Introduction to Git & VCS
- 2. Installation & Configuration
- 3. Git Architecture & Concepts
- 4. Basic Workflow (add/commit)
- 5. Git Log & History
- 6. Branching Basics
- 7. Merging & Conflict Resolution
- 8. Git Rebasing
- 9. Undoing Changes
- 10. Working with Remotes
- 11. Git Stashing
- 12. Git Tagging
- 13. Git Ignore & Attributes
- 14. Advanced Git Tools
- 15. Best Practices & Workflows
14. Advanced Git Tools
Once you master the daily staging, committing, branching, and remote workflow, you are ready to learn Git's **advanced power tools**. These commands are designed to handle highly specific scenarios, recover from seemingly catastrophic mistakes, and debug large project histories.
1. Selective Committing: git cherry-pick
Imagine a colleague has committed a critical bug fix on a separate development branch, but that branch also contains dozens of other incomplete features that aren't ready to merge. You want to pull in **just that one specific commit** without merging the entire branch. This is called **Cherry-picking**.
To cherry-pick a commit, look up its SHA-1 hash and run:
git cherry-pick a8f1b2cGit calculates the exact changes introduced in commit a8f1b2c and creates a **brand new commit** containing those exact modifications on top of your current active branch.
2. The Ultimate Safety Net: git reflog
Have you ever done a hard reset (git reset --hard) and wiped out commits you actually needed? Or deleted a branch containing work you forgot to merge? In many other version control systems, that work is gone forever. In Git, it is almost certainly still recoverable using Reflog.
Git maintains a log of every single action you take locally (switching branches, committing, rebasing, resetting). To view this log:
git reflogThis lists all your recent operations, including their commit hashes, even if those commits are no longer visible in git log:
HEAD@{0}: reset: moving to HEAD~1
HEAD@{1}: commit: Add landing page form fields
HEAD@{2}: commit: Fix logo dimensions
HEAD@{3}: checkout: moving from feature/branding to mainTo recover a commit you deleted or reset over, simply copy its hash from the reflog (e.g., HEAD@{1}) and create a branch there or reset back to it:
# Restore your branch to that exact state
git reset --hard HEAD@{1}3. Debugging via Binary Search: git bisect
If a feature that was working perfectly last week is suddenly broken today, and you don't know which of the 100 commits introduced the bug, git bisect helps you find it using a **binary search algorithm**.
Here is how to use it:
- Start the search process:
git bisect start - Tell Git that your current version is broken (bad):
git bisect bad - Find a commit in your history that you *know* was working perfectly (e.g., commit
e77b0c1from last week) and tell Git:git bisect good e77b0c1
Git will automatically checkout a commit in the exact middle of the range. You run your application, test it, and tell Git either:
git bisect good # If this middle version works fine
# OR
git bisect bad # If this middle version is also brokenGit repeats this binary search, cutting the commits range in half each time, until it successfully identifies the **exact commit** that introduced the bug!
Once you are done debugging, return to your original branch using:
git bisect reset