How to restore a file to a specific state 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
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.
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.
On The Web
See all of the places I have a presence on the web.