Key Takeaways
- Interactive Rebase (
git rebase -i) is the ultimate tool for cleaning up local history. You can squash, reword, drop, and reorder commits. - Never Rebase Public History. If you have pushed it, do not rebase it. Use
git revertinstead. - Reset moves the HEAD pointer.
--soft: Keeps changes staged.--mixed: Keeps changes unstaged (working directory).--hard: Destroys changes.
- Reflog is your safety net. It tracks every movement of HEAD, allowing you to recover “lost” commits and deleted branches.
- Bisect uses binary search to find bugs in a large commit history efficiently (
O(log N)).
Interactive Flashcards
Which command allows you to combine multiple commits into one?
git rebase -i (using "squash" or "fixup")What is the difference between
reset and revert?reset rewrites history (unsafe for public). revert adds a new commit to undo changes (safe for public).You accidentally ran
git reset --hard. How do you recover?Use
git reflog to find the commit hash before the reset, then reset to it.Which reset mode keeps your changes in the Staging Area?
--softHow do you automate finding a bug with a script?
git bisect run <script>What does
git commit --amend do?It modifies the most recent commit (changing the message or adding files). It changes the SHA-1 hash!
Git Advanced Cheat Sheet
| Command | Description |
|---|---|
| Rebasing | |
git rebase -i HEAD~n |
Start interactive rebase for last n commits. |
git rebase --continue |
Continue rebase after resolving conflicts. |
git rebase --abort |
Stop rebase and return to original state. |
| Rewriting | |
git commit --amend |
Edit the last commit message/content. |
git reset --soft HEAD~1 |
Undo commit, keep changes staged. |
git reset --mixed HEAD~1 |
Undo commit, keep changes unstaged. |
git reset --hard HEAD~1 |
Undo commit, destroy changes. |
git revert <commit> |
Create a new commit that undoes changes. |
| Recovery | |
git reflog |
Show log of HEAD movements. |
git reset --hard HEAD@{n} |
Move HEAD to a specific point in reflog. |
| Debugging | |
git bisect start |
Start binary search. |
git bisect good <ref> |
Mark commit as good. |
git bisect bad <ref> |
Mark commit as bad. |
git bisect reset |
Exit bisect mode. |
Glossary
Need a refresher on terms like HEAD, Staging Area, or Detached HEAD?