I deleted the wrong branch and had seconds to fix it.
That’s when git checkout -- saved me. Then I learned something better: git checkout is more than just a way to switch branches. It’s a precision tool for jumping to a commit, restoring files, or even disconnecting your working directory to inspect the past. Yet its power comes with an edge—one wrong command and you overwrite work forever. That’s why mastering Git checkout in the right context matters.
Many confuse git checkout with git switch or git restore, but here’s the truth: git checkout is still widely used because it’s fast and flexible. The trick is to use the correct patterns and know exactly what will be changed before you hit enter.
git checkout <branch> moves your HEAD to another branch, updating your working directory so it matches that commit history. When working with hotfixes or testing a feature, that shift is seamless. If you need to inspect code from a specific commit without switching branches, git checkout <commit hash> lets you time-travel safely. Just remember—you’re in a detached HEAD state, so commits there don’t belong to a branch unless you create one.