10. Advanced Git: Reflog, Bisect, Stash, and Submodules
Git has powerful advanced features for recovering lost commits, finding bugs, saving temporary work, and managing dependencies. Understanding these tools makes you a Git power user. This module covers the advanced Git commands that senior engineers use daily. These are the tools that separate experts from beginners.
The key insight is that Git tracks everything. The reflog records every change to HEAD, allowing you to recover lost commits. Bisect finds the commit that introduced a bug. Stash saves temporary work. Submodules manage dependencies. These tools are essential for complex Git workflows. Understanding them gives you superpowers in Git.
This module covers reflog for recovery, bisect for debugging, stash for temporary work, and submodules for dependencies. Each command is explained with examples and use cases.
Git Reflog: Recovering Lost Commits
The reflog records every change to HEAD. It's your safety net — when you accidentally lose a commit, the reflog can recover it. The reflog is local to your repository and not shared with remotes. Common reflog use cases: recovering after a bad reset, recovering after an interactive rebase, and finding the commit before a force push. The reflog is your best friend when things go wrong.
Whenever you do something that loses commits, the reflog is there to save you. Reset to a previous state? Reflog has the old HEAD. Rebase went wrong? Reflog has the pre-rebase state. Force push overwrote remote history? Reflog has the original commits. The reflog is your insurance against mistakes.
Git Bisect: Finding the Bug
Bisect helps you find the commit that introduced a bug. It uses binary search to efficiently find the culprit. You mark commits as good or bad, and Git narrows down the range. Bisect is essential for debugging regressions. It's much faster than manual search. Use bisect when you know a bug exists and want to find when it was introduced.
Bisect is one of Git's most powerful debugging tools. Instead of checking out commits manually, you let Git do the work. It picks the midpoint, you test, and it narrows down the range. This is much faster than linear search. For a bug introduced in one of 100 commits, bisect finds it in about 7 steps.
Git Stash: Saving Temporary Work
Stash saves your uncommitted changes when you need to switch branches. It's useful for: switching branches without committing, pulling changes when you have uncommitted work, and saving experiments. Stash is a safe place for temporary work. Stash saves both staged and unstaged changes. You can name stashes, apply them later, and even apply only specific files. Stash is essential for workflow flexibility.
Stash is one of the most frequently used Git commands. You're working on something, need to switch branches, and don't want to commit half-finished work. Stash to the rescue. You can have multiple stashes and apply them later. This is essential for context switching in development.
Step-by-Step: Advanced Git Commands
Follow these steps to master reflog, bisect, stash, and submodules.
Part 1: Refloggit initecho <strong><code>First > file.txtgit add . && git commit -m <strong><code>Firstecho <strong><code>Second > file.txtgit add . && git commit -m <strong><code>Secondgit reset --hard HEAD~1 # Oops! We lost the second commit!git reflog # See all HEAD changes, find the lost commit's hashgit reset --hard <lost-commit-hash> # Recover the lost commit
Part 2: Bisectgit bisect startgit bisect bad # Current commit is brokengit bisect good <known-good-commit-hash>
Git checks out a commit in the middle. Test it, then:git bisect good or git bisect bad
Repeat until Git identifies the bug-introducing commit.git bisect reset # End bisect
Part 3: Stashecho <strong><code>WIP changes >> file.txtgit stash # Save changes temporarilygit stash list # See stashesgit stash apply # Restore changesgit stash pop # Restore and remove from stash list
Try It Yourself: Create a situation where you lose a commit, then use reflog to recover it. Use bisect to find a bug in a project. Use stash to save work while switching branches.
Knowledge Check
Ready to test your understanding of 10. Advanced Git: Reflog, Bisect, Stash, and Submodules?