'Git partial/sparse/narrow fetch and update in Azure Devops Hosted Agent
I am wondering if it's possible to fetch only a single file from a Git repository in order to commit a new change to it. We want to change the file on a Azure DevOps Hosted Agent however downloading the entire repo would take a significantly long time, as it is large.
I read of these options:
- --Filter option
- Git sparse checkout (I'm not sure if this is only available on GitHub)
- Microsoft GVFS
Filter command attempt
git clone --depth 1 --filter=sparse:path=ReadMe.md
warning: filtering not recognized by server, ignoring
Sparse checkout
git config core.sparsecheckout true
echo File.txt >> .git/info/sparse-checkout git pull origin master
However it still retrieved everything.
The server repository is running GIT v2.18.
- Is there anything that needs to be configured on the server to make it these work?
- Is the --filter option only available on certain versions?
- Could GVFS achieve this and is it possible to setup on the Hosted Agent?
Thank you.
Solution 1:[1]
- Indeed the filter method will not work. As is further noted in the question you link: - There is no server support as of v2.19.0, but it can already be locally tested. 
- Sparse checkout will still download all the files, it just won't check them out to disk. 
- GVFS requires server changes, and is only supported by Azure Repos. It is not part of stock (you indicate that your Git server is 2.18.) 
As Shayki Abramczyk noted, using a REST API may be your best option. If your hosting provider supports it, you can probably download a file directly from the hosting provider. Many hosting providers will allow you to commit those changes as well.
Solution 2:[2]
The best way to download one file from Git repo is with Azure DevOps Rest API - Items - Get.
GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/items?path={path}&api-version=5.0-preview.1
If you add the parameter download (for example: ?path={path}&download=true) the file will be downloaded on the agent.
So add a task with a simple PowerShell script (with Invoke-RestMethod) and get the file.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source | 
|---|---|
| Solution 1 | Community | 
| Solution 2 | Shayki Abramczyk | 
