Git moves fast. One command can flip your entire workspace from one branch to another, restore files, or checkout a commit from deep in the history. git checkout is that command, and understanding it fully is the difference between control and chaos.
git checkout changes the HEAD in your repository. HEAD points to the current branch or commit. When you run git checkout <branch>, Git will switch your working directory to match the snapshot of that branch. Your files will change to reflect its latest commit. Your staging area resets to match.
You can also use git checkout <commit> to go directly to a past commit. This places you in a “detached HEAD” state, where HEAD points to the commit instead of a branch. This is useful for testing old code or inspecting history without altering branches. But it’s a dead end—unless you make a new branch from there, your changes won’t be saved in a branch.
git checkout -- <file> restores a file from the index or from a target commit. This discards local changes to that file. It’s immediate and irreversible, so use it only when you mean it.