简体   繁体   English

我可以从分支中排除git仓库中的文件吗?

[英]Can I exclude a file in a git repo from branching?

I have a file (specifically my .gitignore file) in the repo that I want to track and push to a central repository, so others will receive a copy when cloning the project. 我在repo中有一个文件(特别是我的.gitignore文件),我想跟踪并推送到中央存储库,所以其他人在克隆项目时会收到​​一份副本。

But I want to exclude the file from being duplicated by branching. 但我想通过分支排除文件被复制。 I want to have only one version stable in all branches, so when I change it in one, I don't have to commit and merge (or cherry-pick) this change to all other branches manually. 我希望在所有分支中只有一个版本稳定,所以当我在一个版本中更改它时,我不必手动提交并合并(或樱桃选择)此更改到所有其他分支。

Is there a way to achieve this with git? 有没有办法用git实现这个目标?

As there are several valid answers posted as comment, I'll self-answer here giving credit to Users Alexey Ten, wonce and Darryl. 由于评论中有几个有效的答案,我会在这里自我回答给予用户Alexey Ten,wince和Darryl。

Setting a file as 'stable' or 'pinned' between branches is probably not possible. 在分支之间将文件设置为“稳定”或“固定”可能是不可能的。

But in the special case of wanting the .gitignore file to be stable, this is most likely a case of wrong workflow. 但是在希望.gitignore文件稳定的特殊情况下,这很可能是错误的工作流程。

Files should be included in the .gitignore file when they are generated, which has to happen in one branch eventually. 生成文件时,文件应包含在.gitignore文件中,最终必须在一个分支中进行。 Other branches will then not have these new files hence no need for a modified .gitignore. 其他分支将没有这些新文件,因此不需要修改.gitignore。 Once other branches also get the new files via merge or rebase, they should also get the modified .gitignore. 一旦其他分支也通过合并或rebase获取新文件,他们也应该获得修改后的.gitignore。 Wanting to merge just the .gitignore file means you probably have added a file also present in another branch manually, which probably was bad practice as you should have created it with a merge from the original branch. 想要合并.gitignore文件意味着您可能已经手动添加了另一个分支中存在的文件,这可能是不好的做法,因为您应该使用原始分支的合并创建它。

A notable exception to this rule is if you want the .gitignore file for a wildcard-rule you previously created in an other branch. 此规则的一个值得注意的例外是,如果您希望.gitignore文件用于先前在其他分支中创建的通配符规则。 Adding a new file matching the wildcard-rule but not necessarily being the same as the file in the original branch is possible. 添加与通配符规则匹配但不一定与原始分支中的文件相同的新文件是可能的。

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

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