Shaun Luttin
Shaun Luttin

Reputation: 141444

Checkout from a commit all files that end with a specific extension

I know how to checkout one file from a commit.

git checkout a0b1c3d -- path/to/some/file.txt

I also know how to checkout multiple files from a commit.

git checkout a0b1c3d -- path/to/some/file.txt path/to/another/file.txt

How can we checkout all the files that end with a certain extension? I have tried the following:

git checkout a0b1c3d -- *.txt
git checkout a0b1c3d -- */**/*.txt

Neither work. Both commands checkout nothing, even though there are *.txt files to checkout from the specified commit.

Checkout all files from a previous commit with a certain file name suggests that there might be a bug in pathspec.

Upvotes: 4

Views: 1055

Answers (2)

jthill
jthill

Reputation: 60235

Quote the glob so your shell passes it along to Git rather than expanding it itself:

git checkout -- \*.txt

which just worked perfectly for me, it checked out all .txt files (at any level, see git's rules for pathname matching on the command line, git help glossary and find pathspec).

Upvotes: 3

Attila
Attila

Reputation: 3406

Using command substitution it is possible to get all the files with a given extension in your repository. This is possible with git ls-tree and grep.

The list of the resulting files can be passed to git checkout.

You could checkout the a0b1c3d version of the files with txt extension using the following command:

git checkout a0b1c3d -- $(git ls-tree --full-tree --name-only -r HEAD | grep .txt)

Upvotes: 0

Related Questions