GIT——从提交中排除/忽略文件
我需要忽略 git 中的文件!我认为忽略实际上并不是正确的词;我希望这些文件位于 git 中,但我不希望能够对它们提交更改。对于某些人来说,这可能看起来很奇怪,但有很多很多情况我需要这个功能。我现在需要它的一个实例是在 CMS 中使用 git 时;如果没有不断更改的文件,CMS 将无法工作,但我不希望在初始提交后提交这些文件。 (使用 SVN 和 Tortoise 很容易做到这一点)。
工作流程:
- 获取运行应用程序所需的所有文件。
- 提交时忽略指定的目录/文件。
这是我尝试过的:
.gitignore
——文件永远不会进入 git。如果文件已经在 缓存 .gitignore 文件不会执行任何操作。/.git/info/exclude
-- 与 .gitignore 相同的问题,但仅适用于 本地仓库。- 分支--master =>本地忽略=>工作部门。当 工作分支与主分支合并,所做的更改来自 LocalIgnores 最终在 master 中。此外,当您结帐其中一项时 新分支时,已删除的文件将被删除而不是被忽略。
- 第三方文件结构——根目录下的第三方目录 节点保存所有重要第三方文件的副本,以便他们可以 复制到使用 .gitignore 的工作目录中 文件。 (这个可行,但必须有一个更简单/更好的 溶液)。
I need to ignore files in git! I don't think ignore is the right word actually; I want the files to be in git, but I don't want to be able to commit changes to them. This may seem like a weird thing to some, but there are many, many instances that I need this functionality. The one instance I need it for now is when using git with a CMS; the CMS won't work without files that it is constantly changing, but I don't want those files to ever be committed after the initial commit. (This is very easy to do with SVN and Tortoise).
WorkFlow:
- Get All Files needed to run the app.
- Ignore specified directories / files when committing.
Here is what I've tried:
.gitignore
-- the files never enter git. If the file is already in
cache the .gitignore file doesn't do anything./.git/info/exclude
-- same problem as .gitignore, but only for
the local repo.- Branching -- master => LocalIgnores => WorkingBranch. When the
Working branch is merged with master, the changes made from
LocalIgnores end up in master. Also, when you checkout one of the
new branches, the deleted files get deleted instead of ignored. - Thirdparty File Structure -- Thirdparty directory on the root
node that holds a copy of all important thirdparty files so they can
be copied into the working directory that utilizes the .gitignore
file. (This one works, but there has to be an easier / better
solurtion).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我在类似问题中的回答。
Here is my answer from a similar question.
另一种解决方案是使用
pre-commit
挂钩。有关详细信息,请参阅git help hooks
。这更难设置:你必须编写一个 shell 脚本来准确表达你想要允许或禁止的内容,这可能意味着你需要很好地掌握 git。
它更加持久和灵活:使用像 git update-index 这样的技巧,你可能会不小心忘记运行它等等。但是一旦你设置了一个钩子,你就再也不用担心它了。
与建议的其他技巧一样,挂钩不会自动在存储库之间传播,但您可以将它们作为常规文件签入,并让每个人将它们链接到
.git/hooks
。Another solution is to use a
pre-commit
hook. Seegit help hooks
for details.This is harder to set up: you have to write a shell script to express exactly what you want to allow or disallow, which probably means you need to have a fairly good grasp of git.
It's more permanent and flexible: with tricks like
git update-index
you can accidentally forget to run it, etc. But once you set up a hook, you never have to worry about it again.Like the other tricks suggested, hooks don't automatically get propagated between repositories, but you can check them in as regular files and have everyone link them into
.git/hooks
.