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.
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
andgrep
:Note this expects a remote
origin
in amaster
branch. Also you must provide the right filter/extension togrep
.You don't need find or sed, you can use wildcards as git understands them (doesn't depend on your shell):
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) :
This, of course, will irremediably erase your changes!
No; git operates at a whole-repository (and whole-history) level; there's no way of getting a partial checkout of a repository. You can of course check out the repository and then delete everything that doesn't match your file, but of course you gain virtually nothing by doing so.
Dadaso's answer
git checkout -- "*.xml"
checks out all .xml files recursively from index to working directory.However for some reasons
git checkout branch-name -- "*.xml"
(checking out files frombranch-name
branch) doesn't work recursively and only checks "xml" files in root directory.So IMO the best is to use
git ls-tree
then filter file names you are interested in and pass it togit checkout branch-name --
. Here are the commands you can use:Bash (and git bash on windows) version:
cmd (windows) version (if you don't have "C:\Program Files\Git\usr\bin" in you PATH):
for powershell it's still better to call
cmd.exe
because it's much faster (powershell doesn't have good support for native stdin/stdout pipelining):However you you have small number of files you can try this in powershell (like in @aoetalks answer). But I found it extremely slow for couple of houndeds files:
On PowerShell (Windows, haven't tried PowerShell Core+Linux), I was able to do it like this
git ls-tree master -r --name-only | sls ".cscfg" | foreach { git checkout origin/master -- $_ }