如何使用git仅签出具有给定文件扩展名的文件及其父文件夹?

如何使用git仅签出具有给定文件扩展名的文件及其父文件夹?,git,search,ant,git-checkout,Git,Search,Ant,Git Checkout,我们将在TeamCity下运行的Ant构建脚本中使用它。(在git术语中,“签出”的意思是“克隆”——即,您当前没有存储库的副本,需要从远程存储库获取一些文件。) 简而言之,你不能 您可以通过一些限制在git中进行浅层克隆(仅获取最后几个版本),但不能轻松地进行窄层克隆(仅获取存储库的某些部分,例如一个子目录,或仅获取符合特定条件的文件) 在某种程度上,这实际上是git作为分布式版本控制系统的一个特性:当您克隆了一个存储库时,您就知道您已经拥有了完整的历史记录、所有的分支,以及处理代码所需的一切

我们将在TeamCity下运行的Ant构建脚本中使用它。

(在git术语中,“签出”的意思是“克隆”——即,您当前没有存储库的副本,需要从远程存储库获取一些文件。)

简而言之,你不能

您可以通过一些限制在git中进行浅层克隆(仅获取最后几个版本),但不能轻松地进行窄层克隆(仅获取存储库的某些部分,例如一个子目录,或仅获取符合特定条件的文件)

在某种程度上,这实际上是git作为分布式版本控制系统的一个特性:当您克隆了一个存储库时,您就知道您已经拥有了完整的历史记录、所有的分支,以及处理代码所需的一切,这些都是完全独立的

当然,围绕这一点有多种方法,例如:

  • 您可以使用
    git-archive--remote=
    获取远程存储库的tar存档,并将其传送到
    tar-x--wildcards--no-archored'*.随便什么'
  • 只需在本地的其他地方克隆完整的存储库,让构建脚本更新它并复制所需的文件即可
  • 等等等等

  • 我就是这么做的,而且效果很好。我只需要编辑项目中的标记(extension.md)文件

    #clone as usual
    git clone https://github.com/username/repo.git myrepo
    
    #change into the myrepo directory that was just created 
    cd myrepo
    
    #turn off tracking for everything
    #this allows us to start pruning our working directory without the index 
    #being effected, leaving us only with the files that we want to work on
    git ls-files | tr '\n' '\0' | xargs -0 git update-index --assume-unchanged
    
    #turn on tracking for only the files that you want, editing the grep pattern
    # as needed
    #here I'm only going to track markdown files with a *.md extension
    #notice the '--no-assume-unchanged' flag
    git ls-files | grep \\.md | tr '\n' '\0' | xargs -0 git update-index --no-assume-unchanged
    
    #delete everything in the directory that you don't want by reversing 
    #the grep pattern and adding a 'rm -rf' command
    git ls-files | grep -v \\.md | tr '\n' '\0' | xargs -0 rm -rf
    
    #delete empty directories (optional)
    #run the following command. you'll receive a lot of 'no such file or 
    #directory' messages. run the command again until you 
    #no longer receive such messages.you'll need to do this several times depending on the depth of your directory structure. perfect place for a while loop if your scripting this
    find . -type d -empty -exec rm -rf {} \;
    
    #list the file paths that are left to verify everything went as expected
    find -type f | grep -v .git
    
    #run a git status to make sure the index doesn't show anything being deleted
        git status
    
    你应该看到:

    # On branch master
    nothing to commit, working directory clean
    
    完成了

    现在,您可以像处理文件一样处理这些文件 你检查了所有东西,包括拉和推到遥控器和遥控器
    它将只更新您签出的文件,而不删除其余文件。

    我们目前使用Subversion(很快将迁移到Git),我们被迫签出SVN存储库的大型平板(目录列表),然后使用Ant文件模式进行搜索。Subversion API不支持这种类型的基于模式的搜索和签出。我还没有检查最新的v1.7,但我们正在转向Git。不久前,我向Subversion团队提交了一个基于模式的签出请求,但我想知道Git是否已经这样做了。或者,可能还有另一种基于生成git命令的方法。我自己才刚开始使用SVN,所以我不确定对不起。谢谢Mark。我意识到我的要求与GIT的初衷背道而驰。我还需要做一些关于TeamCity如何使用GIT克隆的家庭作业。我想你的建议在这方面可能有用。