Git:如何忽略特定远程的文件?

huangapple go评论64阅读模式
英文:

Git: How to ignore files for a specific remote?

问题

例如,如果一个仓库有两个`origin````bash
cd my-repo
git remote add origin https://example.com/shared/repo.git
git remote add origin-private https://example.com/private/repo.git

并且在my-repo目录下有两个配置文件,分别命名为public.template.envJohn.env

现在,如果我们需要确保:第一个文件可以发布到两个origin,而第二个文件只能推送到origin-private

有没有办法做到这一点?

似乎这个与这里的设置非常接近,在一些搜索后。然而,它建议Git没有办法做到这一点。但这是一个旧帖子,不知道是否有所改变。


<details>
<summary>英文:</summary>

For instance, if there are two `origin`s for a repo:
```bash
cd my-repo
git remote add origin https://example.com/shared/repo.git
git remote add origin-private https://example.com/private/repo.git

and there are two configuration files under the dir my-repo, named public.template.env and John.env.

Now, if we need to ensure: the first one is ok to publish to both origins, while the 2nd one should be pushed to the origin-private only.

Is there a way to do that ?

It seems this one is very close to the setting here, after some Googlings. However, it suggest there is no way to do that with Git. But it is a old post, wondering if there are changes about it.

答案1

得分: 1

通常情况下,当你在.gitignore中忽略文件时,这告诉Git如果这些文件出现在工作树中并且尚未出现在索引中(除非你强制操作),就不要提交这些文件。一旦文件被提交,它就会出现在历史记录中。

为了推送一个不包含该文件的历史,你必须有一个不包含该文件的单独历史,这意味着这两个历史记录将完全分歧,并且除了在创建该文件之前的提交之外,它们不会共享任何提交。没有办法只推送不包含该文件的相同历史记录,因为远程端会检测到你没有发送该文件,历史记录将会损坏。

因此,你想要做的事情在功能上非常困难,因为它涉及在同一个仓库中维护两个完全不同的历史记录,而实际上人们不会这样做。如果你愿意,你可以创建一个包含一些私有文件的私有仓库的子模块,并发布公共仓库,而不发布私有仓库。人们会知道有私有文件,但无法访问它们。

英文:

Typically, when you ignore files with .gitignore, that tells Git not to commit those files if they appear in the working tree and don't already appear in the index (unless you force the operation). Once a file is committed, it's in the history.

In order to push a history without that file, you'd have to have a separate history without that file, which would mean that the two histories would completely diverge and wouldn't share any commits in common, except for the commits before that file was created. There's no way to push only the same history without that file, because the remote side would detect that you hadn't sent that file and the history would be corrupt.

Thus, what you want to do is functionally very difficult because it involves keeping two completely different histories in the same repository, and as a practical matter people don't do it for that reason. If you want, you can create a submodule of a private repository containing some private files, and publish the public one, while not publishing the private one. People would the know that there were private files, but wouldn't be able to access them.

huangapple
  • 本文由 发表于 2023年6月5日 21:25:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76406887.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定