git checkout --<pathname>
This command is useful for discarding local changes to a specific file or directory by resetting it to the version that Git currently tracks, without affecting other files or your commits.
This command uses checkout with -- to clearly separate options from the path you want to restore, ensuring Git treats <pathname> as a file or directory rather than a branch or tag; the -- is not a flag with its own behavior, but a standard way in many CLI tools to say “everything after this is a path,” and <pathname> is the file or directory you want to reset, such as src/app.js or config/settings.json. When run, this command replaces the content of <pathname> in your working directory with the version from the index (staging area) or, if unchanged in the index, from the current HEAD commit, effectively discarding any uncommitted edits you made to that path. If you had staged changes to <pathname>, this command will restore it to the staged version, leaving your commit history intact but losing the unstaged edits for that file.
You can use closely related forms like git checkout <branch> -- <pathname> to restore a file from another branch (for example, git checkout main -- src/app.js to grab src/app.js as it exists on main) or git restore <pathname> which is the newer, clearer alternative for resetting a file to its current HEAD version. To undo both staged and unstaged changes, you might use git restore --staged <pathname> followed by git restore <pathname>, or the older equivalent git reset HEAD <pathname> combined with this command; for resetting entire directories or the whole project, variations like git checkout -- . or git restore . operate on all tracked files.
Examples:
git checkout -- src/app.jsgit checkout -- config/settings.jsongit checkout -- README.md