我们将在 TeamCity 下运行的 Ant 构建脚本中使用它。
问问题
278 次
2 回答
1
(通过“签出”我假设您的意思是 git 术语中的“克隆” - 即您当前没有存储库的副本,并且需要从远程存储库获取一些文件。)
简短的回答是你不能。
有一些限制,您可以在 git 中进行浅克隆(仅获取最后几个版本),但您不能轻松进行窄克隆(仅获取存储库的某些部分,例如一个子目录,或者仅获取与特定文件匹配的文件标准)。
在某种程度上,这实际上是 git 作为分布式版本控制系统的一个特性:当你克隆了一个存储库时,你知道你已经拥有了完整的历史记录、所有的分支,以及你完全处理代码所需的一切独立的。
当然,有多种方法可以解决这个问题,例如:
- 您可以
git archive --remote=<repo>
用来获取远程存储库的 tar 存档,并将其通过管道传输到tar -x --wildcards --no-anchored '*.whatever'
- 只需在本地其他地方克隆完整的存储库,然后让您的构建脚本更新它并仅复制您想要的文件
- 等等等等
于 2011-08-18T09:48:27.540 回答
1
这就是我所做的,而且效果很好。我只需要编辑项目中的降价(扩展名 .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
完毕!
现在,您可以像检查所有内容一样使用这些文件,包括执行拉取和推送到远程,它只会更新您签出的文件而不删除其余文件。
于 2014-05-26T02:29:00.263 回答