Curiously Chase

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

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 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.

Share on Twitter