Once you start collaborating with other developers, it’s going to be important to know how to revert a single file to a certain commit in Git. Sometimes you’ll need to change files not related to your pull request in order to test the feature you’re working on.
Git Revert a File Steps
- Find the commit ID of the version of the file you want to revert to.
- Find the path to the file you want to revert from the working directory.
- In the terminal, change directories to the working directory.
- Type
git checkout [commit ID] -- path/to/file
and hit enter. - Commit the change to the reverted file.
However, manually changing each line of code in those files back to their original state and doing a new commit can lead to a messy commit history. Reverting the file is a much cleaner way of handling it.
4 Steps to Revert a File in Git
1. Find the Commit ID
First, you need to go to the shared repository on GitHub and find the file that you want to revert. Once you navigate to the file, you should see this right above the file:
On the right hand side, you can see a seven digit commit ID and a date. That is the commit ID for the most recent commit in which that file was modified. Either write this commit ID down, or copy it to your clipboard.
2. Find the File Path
The next thing you need is the path to the file from the working directory. This part is easy because the path to the file is on the same GitHub screen where you found the commit ID for the file.
Above, you can see the same screenshot from before, but this time I’ve underlined the file path. Notice, I only underlined part of the path. The first directory listed is the working directory name, and will be the directory you’re in when using this file path. Because of this, you only want the underlined portion.
3. Revert the File
All that is left is to revert the file. Once you’ve opened a terminal and changed to the working directory, you use git checkout
to revert the file. The format of the Git command will look like this:
git checkout [commit ID] -- path/to/file
If I were going to revert the file in the screenshots above, that would look like this:
4. Commit the Change
I know what you’re thinking, “Wait a minute, I thought the whole point was to not create a new commit?” Well, that’s half true. We didn’t want a new commit for the file we reverted. But once we revert the file, we need to commit that change. In this case, the change is a revert of a single file. This done with the standard commit command:
git commit -m 'commit message'
Then you can push that commit to the remote so that the version of your branch on GitHub matches your local version.
Git Revert a File Using Git Restore
Before we wrap things up, let’s look at how to revert a single file to remove uncommitted local changes. This is where git restore
comes in handy.
git restore
is a very useful command that can unstage a file, or even remove all changes that have been made locally since that last commit, thus restoring the file.
For example, let’s say you’ve made changes to multiple files and staged all these modified files with the command git add
. Afterward, you realize that you didn’t want the file named Index.js
to be part of this commit. You can unstage that file with the command:
git restore --staged index.js
Once the file is unstaged, you can fully restore it to the previous commit with the command:
git restore index.js
Frequently Asked Questions
How do you revert a single file in Git?
- Find the commit ID of the version of the file you want to revert to.
- Find the path to the file you want to revert from the working directory.
- In the terminal, change directories to the working directory.
- Type
git checkout [commit ID] -- path/to/file
and hit enter. - Commit the change to the reverted file.
How do you restore a file in Git?
If you want to restore a file in Git, you can use the command git restore
. This allows you to unstage a file or remove all changes locally. For example you can enter:
git restore --staged index.js
To unstage the file index.js
from your commit. If you’d like to fully restore the file to the previous commit, you can use:
git restore index.js