Restore Previous Commit Without Changing HEAD
Goal
Restore files from a previous commit to your working directory while keeping HEAD on the current branch (avoiding detached HEAD state).
Find the Target Commit
git log --oneline -n <N>
Restore Files from Previous Commit
git checkout <commit-hash> -- .
This command:
- Updates all files in current directory and subdirectories to match the specified commit
- Leaves HEAD on your current branch (no branch switching)
- Only affects working directory, not branch pointer
Without -- .: Git checks out the entire commit and enters detached HEAD state.