Member-only story
Lifesaving “Git” Commands: Mastering Emergency Situations
Introduction
Git, the widely used version control system, is a powerhouse tool for developers. It allows seamless tracking of changes, collaborative work on projects, and maintaining a complete history of all edits and updates. However, sometimes things can go wrong — a wrong branch merge, accidental commit deletions, or other mishaps. Knowing some advanced Git commands can save the day. Here’s a guide to some of the “secret” lifesaving Git commands that every developer should know.
1. git reflog
— Recover Lost Commits
One of Git’s most powerful features for disaster recovery is the reference log (reflog
). This command provides a ledger of all the changes that have updated the head of branches in your repo. It’s invaluable for finding lost commits, especially if you've rebased or amended history.
Usage Example:
git reflog
git reset --hard HEAD@{index} # Reset to a particular state in history
2. git reset
— Undoing Changes
If recent commits are causing issues or weren’t intended, git reset
is your go-to command. It allows you to roll back to a specific state. Depending on the flag (--soft
, --mixed
, --hard
), it can unstage changes, revert to previous commits, and discard all history of changes.
Usage Example:
git reset --hard HEAD~1 # Revert the most recent…