Reset specific file to its state on a commit or branch with git

How to restore a file to a specific state with git.

thumbnail for Reset specific file to its state on a commit or branch with git

If you are working on a file that's version controlled with git and you realize, "whoops! I want to reset that file back to the way it was in a specific commit" or "I want to reset that file back to its state in my mainline branch", the command you want to use is checkout.

Let's say I want to reset a file named some/changed/file.ts back to the state it was in on my main branch, I would run:

 git checkout origin/main some/changed/file.ts 

This would restore the file back to the state it was originally in. If you've committed changes to it, you'll see that your git status shows that the file has changed by adding anything back and removing anything that was added.

Was this page helpful?

Subscribe to my Newsletter

Every other week I publish the Curiously Crafted newsletter.

In it, I explore the intersection of curiosity and craft: the people who make stuff, what they make and the way they pursue the craft of making.

The curious logo of Chase Adams: glasses and a bow tie.stay curious.