Git Checkout Using Pathspec with Wildcard

Viewed 37

I would like to restore some files in the working directory to the state of the latest commit on the current local branch, which is master, however, my attempts to use git checkout to achieve this have failed consistently.

I have confirmed that the following works: git checkout HEAD src/graphql/User.ts

But I know that I should be able to use a wildcard in the pathspec, i.e. : git checkout HEAD '*/User.ts'

However this fails.

How can I use a pathspec with a wildcard to restore a limited set of files to the state stored in the latest or previous commit?

1 Answers

As it turns out, there are two solutions to this problem.

1. Git has shipped a new feature called restore which allows for the use of a pathspec. See (https://git-scm.com/docs/git-restore). So

git restore */User.ts

will work.

2. The following will work as well, although I'm not quite sure why:

git checkout -- '*/User.ts'

Related