Last Updated: January 3, 2026
You’ve been working on a feature branch for days, fine-tuning your code, and testing new ideas. Suddenly, you realize that the last change you made to a crucial file introduced a bug. You want to revert that single file to its last committed state without affecting any other changes in your working directory.
What do you do?
This is where git checkout for files comes into play. Understanding this command is essential for effectively managing changes in your Git workflow.
git checkout is a versatile command in Git, often used to switch branches or restore files. When used to checkout files, it allows you to revert changes in your working directory to the state of a specific commit, typically the most recent commit on the current branch.
This can be a lifesaver when you need to discard unwanted local modifications quickly.
To fully grasp how git checkout operates, it's important to understand Git's object model. Git stores your project in three main areas:
When you use git checkout to revert a file, Git retrieves the version of that file from the repository and replaces the version in your working directory. This action affects only the working directory, leaving your staging area and repository intact unless you specify otherwise.
To checkout a specific file, the command syntax is straightforward:
This command will replace the version of <file> in your working directory with the last committed version from the current branch.
Imagine you have a file named app.js, and you have made changes that you no longer want.
Check the status of your working directory to see the modified file:
If app.js is listed as modified, you can revert it:
Now when you check the status again, app.js is no longer listed as modified. The file has been restored to its last committed state.
This action is irreversible; once you checkout the file, any uncommitted changes will be lost.
You can also use git checkout to restore a file from a specific commit, not just the latest one. This can be useful if you want to revert to an earlier version of a file.
First, you need to find the commit hash that contains the version of the file you want. You can use:
This will show you a list of commits along with their hashes.
Once you have the commit hash, you can restore the file:
For instance:
This command fetches app.js from the commit with hash a1b2c3d and places it in your working directory.
Use the -- separator to distinguish between the commit hash and the file name. This is crucial when the file name might resemble a commit hash.
Understanding when and why to use git checkout for files can help streamline your workflow.
If you've been experimenting with changes and want to revert a single file back to its last committed state, using git checkout <file> is the quickest method. This is especially useful in larger projects where reverting entire commits isn't feasible.
Use git checkout <commit_hash> -- <file> when you want to assess how a file looked at a particular point in time. This can aid in debugging or retrieving useful code that might have been mistakenly removed.
If you've merged a branch and noticed that a specific file from the merge introduced issues, you can use git checkout to revert just that file back to its state before the merge, rather than undoing the entire merge.
Although git checkout is powerful, it does have some limitations.
As mentioned earlier, using git checkout to revert files discards all uncommitted changes. If you're unsure, consider using git stash first to save your changes temporarily.
If you have uncommitted changes in the file you're trying to checkout, Git will refuse to perform the operation unless you use the -f (force) flag. For example:
This will discard local changes in app.js and restore it to the last committed version. Use this option with caution.
Using the force option will lead to the loss of any modifications made to the file since the last commit.
Now that you have a solid understanding of how to use git checkout to manage and restore individual files, you are ready to explore its replacement: git restore.
This new command offers a more intuitive approach to file management in Git, providing clearer options for undoing changes. In the next chapter, we will discover how git restore simplifies these tasks and enhances your overall Git experience.