简体   繁体   English

在 git 中获取/拉取时如何忽略其他目录?

[英]How do I ignore other directories when fetching/pulling in git?

Originally, our git repository had one subdirectory in it.最初,我们的 git 存储库中有一个子目录。 This was the code to our website.这是我们网站的代码。 Now we want to include associated files and keep it all in one repository, but for our production server, we just want to git pull/fetch that original subdirectory (ie code/ ).现在我们想要包含相关文件并将它们全部保存在一个存储库中,但是对于我们的生产服务器,我们只想git pull/fetch原始子目录(即code/ )。

How do I configure git to ignore all other directories when doing a git fetch or git pull ?在执行git fetchgit pull时,如何配置git以忽略所有其他目录?

I don't want to reclone;我不想重新克隆; most of the solutions I've found assume that I'm starting from scratch.我找到的大多数解决方案都假设我是从头开始的。

First, a word of caution: Each of the first two comments on the question has bad advice[1].首先,提醒一句:对这个问题的前两条评论中的每一条都有不好的建议[1]。

So... fetch does not update working files.所以... fetch不会更新工作文件。 pull does a fetch and then a merge to HEAD (roughly), and really its the checkout of the updated HEAD that normally updates the working directories. pull执行一次提取,然后合并到HEAD (大致),实际上它是通常更新工作目录的更新HEAD的检出。

You can set up "sparse checkout", but paths are still relative to the worktree root.您可以设置“稀疏结帐”,但路径仍然相对于工作树根。 That is, if you have也就是说,如果你有

/
  /other-artifacts
  /website
  file

and you have your repo on the production server at /repo , you can set up a sparse checkout to give you /repo/website/* , but if you want the contents of website to appear directly in /repo that's another story.并且您在/repo的生产服务器上拥有您的存储库,您可以设置一个稀疏结帐来为您提供/repo/website/* ,但是如果您希望website的内容直接出现在/repo ,那就是另一回事了。

The statement "I don't want to re-clone" may or may not constrain your options; “我不想重新克隆”这一声明可能会也可能不会限制您的选择; without better understanding the reason, I can't really address that.没有更好地理解原因,我无法真正解决这个问题。 Assuming the repo worktree currently is the production website directory, recloning to a new location is likely (a part of) the easiest way to get from where you are to where you want to be, though moving and/or reconfiguring the existing repo might be doable.假设 repo 工作树当前生产网站目录,重新克隆到新位置可能是(一部分)从您所在的位置到您想要的位置的最简单方法,尽管移动和/或重新配置现有的 repo 可能是可行。

What I'm getting at is, if the production directory is to be a subtree of the repo's worktree, then you probably need your repo worktree to be somewhere else on the file system.我的意思是,如果生产目录是 repo 工作树的子树,那么您可能需要将 repo 工作树放在文件系统上的其他位置。 Then the production directory could be replaced with a symlink to the correct part of the worktree;然后可以用指向工作树正确部分的符号链接替换生产目录; or you could udpate it after each checkout (using a post-checkout hook).或者您可以在每次结帐后更新它(使用结帐后挂钩)。

Another fundamentally different approach would be to put the website itself in a separate repo and make it a submodule of the broader repo.另一种根本不同的方法是将网站本身放在一个单独的 repo 中,并使其成为更广泛 repo 的子模块。 Many people get nervous when you start talking about submodules, and this would be considerable rework - more than just recloning for sure.当你开始谈论子模块时,很多人会感到紧张,这将是相当大的返工——肯定不仅仅是重新克隆。

It may seem like using a separate branch for the production site (as suggested in comments) would be a "poor man's" approach to approximating a submodule;对生产站点使用单独的分支(如评论中所建议的)似乎是一种近似子模块的“穷人”方法; but doing something like that is just asking for trouble, as any commit on the 'production only' branch would be seen as having deleted everything outside the website folder, relative to any reachable commit from the master branch.但这样做只是自找麻烦,因为“仅限生产”分支上的任何提交都将被视为删除了网站文件夹之外的所有内容,相对于来自主分支的任何可访问提交。


[1] I can only assume the authors know that they aren't really giving well-thought-out advice, since they've opted to "answer" in comments rather than come down here in the answer section where they're subject to voting. [1] 我只能假设作者知道他们并没有真正给出深思熟虑的建议,因为他们选择在评论中“回答”,而不是在回答部分他们受制于表决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM