Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git checkout only certain file types for entire project

Tags:

git

Is there a way to perform a git checkout for only certain file types (.xlf), which recurses down through the entire repository? The results should contain the struture of the repository, meaning the folders, and the contained files of a certain extension.

Repo A

file.xlf
file.txt
level2/
    file2.xlf
    file2.txt
    level3/
        file3.xlf
        file3.txt 

After checkout repo B looks like:

Repo B

file.xlf
    /level2
    file2.xlf
        /level3
        file3.xlf

This is what I have so far:

$ git checkout FETCH_HEAD -- '*.xlf'

This gives all of the ".xlf" files at the root level, but is not recursive down to subdirectories.

Thank you for the help.

like image 335
user5088790 Avatar asked Jul 07 '15 10:07

user5088790


People also ask

Did not match any file known to git checkout?

While getting error “did not match any file s known to git”, You can try git fetch so that your local repository gets all the new info from github. It just takes the information about new branches and no actual code. After that, the git checkout should work fine.

What is filtering content git clone?

In git you can define "filters" that affect the process of moving files from the index to the work tree ("smudge" filters) and from the work tree to the index ("clean" filters). Typically you'll find a . gitattribute file that associates the filters with files at specific paths.

What is git partial clone?

Partial clone is a performance optimization that “allows Git to function without having a complete copy of the repository. The goal of this work is to allow Git better handle extremely large repositories.” Git 2.22. 0 or later is required.


2 Answers

You don't need find or sed, you can use wildcards as git understands them (doesn't depend on your shell):

git checkout -- "*.xml"

The quotes will prevent your shell to expand the command to only files in the current directory before its execution.

You can also disable shell glob expansion (with bash) :

set -f
git checkout -- *.xml

This, of course, will irremediably erase your changes!

like image 59
Dadaso Zanzane Avatar answered Oct 12 '22 00:10

Dadaso Zanzane


UPDATE: Check Dadaso's answer for a solution that will work in most of cases.

You can try something like this, using git ls-tree and grep:

git checkout origin/master -- `git ls-tree origin/master -r --name-only | grep ".xlf"`

Note this expects a remote origin in a master branch. Also you must provide the right filter/extension to grep.

Before this command, you should have done something like this:

git init
git remote add origin <project.git>
git fetch
like image 39
eduludi Avatar answered Oct 12 '22 01:10

eduludi