简体   繁体   English

如何从版本控制中排除文件/文件夹,但将其保留在Git的存储库中?

[英]How to except a file/folder from versioning, but keep it in the repository in Git?

With git rm <file> a file can be deleted locally, removed from the versioning, and deleted on the repository. 使用git rm <file>可以在本地删除文件,从版本控制中删除文件以及在存储库中删除文件。 git rm --cached <file> does nearly the same, but keeps the file in the local working copy. git rm --cached <file>几乎相同,但是将文件保留在本地工作副本中。

Is there possible, to remove a file/folder from the versioning (so its changes are ignored) keeping it in the repository, so that if somebody clones the repo, the file is also there, but cannot be changed with a simple add & commit ? 是否有可能从版本控制中删除文件/文件夹(以便忽略其更改),将其保留在存储库中,这样,如果有人克隆了仓库,文件也在那里,但是不能通过简单的addcommit来更改?

The short answer is no. 最简洁的答案是不。 The .gitignore facilities only prevent files from being tracked in the first place. .gitignore工具仅阻止首先跟踪文件。 In other words, it prevents files from entering the repository for the first time. 换句话说,它防止文件第一次进入存储库。 However, once a file is tracked, it is tracked forever. 但是,一旦文件被跟踪,便会永远被跟踪。 All changes to that file will be tracked. 将对该文件的所有更改进行跟踪。

There are two general solutions. 有两种通用解决方案。 (1) Avoid adding that file to the index ever. (1)避免将该文件添加到索引中。 This is just a matter of policy. 这只是政策问题。 (2) Place all such ignored files in an archive like UnzipMeRightAfterCloning.zip . (2)将所有这些被忽略的文件放在UnzipMeRightAfterCloning.zip类的存档中。 It's an extra step for people getting into the repo, but at least the target files will never be committed. 对于人们进入仓库,这是一个额外的步骤,但是至少永远不会提交目标文件。

Files already under Git you can ignore using 已经在Git下的文件您可以忽略使用

git update-index --assume-unchanged filename 

and unigtore git update-index --no-assume-unchanged filename 和unigtore git update-index-无假设不变的文件名

but, there also some interesting features, git - ignoring commited files and switch branch 但是,还有一些有趣的功能git-忽略提交的文件和switch分支

尝试将文件/文件夹添加到.gitignore

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

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