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

发布于 2024-11-29 22:11:20 字数 39 浏览 1 评论 0原文

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

We would be using this inside an Ant build script running under TeamCity.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花心好男孩 2024-12-06 22:11:20

这就是我所做的并且效果很好。我只需要编辑项目中的 markdown(扩展名 .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

完成!

现在您可以像以前一样使用这些文件
你已经检查了一切,包括对遥控器进行拉动和推送
它只会更新您签出的文件,而不会删除其余文件。

This is what I did and it works pretty well. I had a need to edit only the markdown (extension .md) files within a project.

#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

you should see:

# On branch master
nothing to commit, working directory clean

done!

Now you can work with these files just as you would as if
you had everything checked out, including doing a pull and push to a remote and
it will only update the files that you checked out without deleting the rest.

魔法唧唧 2024-12-06 22:11:20

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

简短的答案是您可以' t。

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

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

当然,有多种方法可以解决这个问题,例如:

  1. 您可以使用 git archive --remote=来获取远程存储库的 tar 存档,并将其通过管道传输到 tar -x --wildcards --no-anchored '*.whatever'
  2. 只需将完整的存储库克隆到本地其他位置,然后让您的构建脚本更新它并仅复制您想要的文件
  3. 等。

(By "checkout" I assume that you mean "clone" in git terminology - i.e. you currently don't have a copy of the repository, and need to get some files from a remote repository.)

The short answer is that you can't.

You can, with some restrictions, do shallow clones in git (only getting the last few versions), but you can't easily do narrow clones (grabbing only some parts of the repository, such as one subdirectory, or only files that match particular criteria).

In a way, this is actually a feature of git as a distributed version control system: when you've cloned a repository you know that you've got the complete history, all the branches, and everything you need to work on the code completely standalone.

There are, of course, various ways around this, for example:

  1. you could use git archive --remote=<repo> to fetch a tar archive of the remote repository, and pipe that to tar -x --wildcards --no-anchored '*.whatever'
  2. just clone the complete repository somewhere else locally, and have your build script update it and copy over just the files you want
  3. etc. etc.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文