Published on

Reverting a File to a Specific Revision in Git

Last Modified on
Last modified on
Reverting a File to a Specific Revision in Git
Photo by Egor Vikhrev on Unsplash

Sometimes when you are working on an application using Git, you made a change to a specific file, committed it, and then pushed the commit to remote. But later, you decided that you wanted to bring back that file to the state previous to that change you made. In other words, perhaps you want to re-insert the code that you had removed, for instance.

That is exactly what happened with me a little while ago. I found that I still needed the code I had removed from a file, and wanted to revert to the file state which still contained that code. It's very simple really, especially if a particular commit only involved that one file. This is the command I executed to revert to the file state that contained the code I wanted to bring back:

git checkout cf0d404 src/index.js

What I am doing here is checking out into a specific commit with the hash cf0d404, and specifically into the file path src/index.js that I want to restore. When I hit return, I see that the file has indeed reverted to the state I want. Then, when I execute the Git command:

git status

The following is returned in Terminal:

git status                                                                                  ✚
On branch handling-express-errors
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   src/index.js

Since I do want this change to remain staged and actually commit it, I simply execute the command git commit, create and save my commit message, and then return to Terminal from VIM.

And that is it!

I will be embedding this episode of Plugging in The Holes along with a transcript in the form of a post on for your hearing and reading pleasure. Bye for now!