GIT——从提交中排除/忽略文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7070659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
GIT -- Exclude / Ignore Files from commit
提问by Patrick
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).
我需要忽略 git 中的文件!我不认为忽略实际上是正确的词;我希望文件在 git 中,但我不希望能够对它们进行更改。这对某些人来说可能看起来很奇怪,但是有很多很多情况我需要这个功能。我现在需要它的一个实例是在 CMS 中使用 git 时;如果没有它不断变化的文件,CMS 将无法工作,但我不希望这些文件在初始提交后被提交。(使用 SVN 和 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).
.gitignore
-- 文件永远不会进入 git。如果文件已在缓存中,则 .gitignore 文件不会执行任何操作。/.git/info/exclude
-- 与 .gitignore 相同的问题,但仅适用于本地存储库。- 分支——master => LocalIgnores => WorkingBranch。当 Working 分支与 master 合并时,LocalIgnores 所做的更改最终会在 master 中。此外,当您签出新分支之一时,已删除的文件将被删除而不是被忽略。
- 第三方文件结构 - 根节点上的第三方目录,其中包含所有重要第三方文件的副本,因此可以将它们复制到使用 .gitignore 文件的工作目录中。(这个有效,但必须有一个更容易/更好的解决方案)。
回答by Andy
Here is my answer from a similar question.
这是我在一个类似问题中的回答。
git update-indexshould do what you want
This will tell git you want to start ignoring the changes to the file
git update-index --assume-unchanged path/to/file
When you want to start keeping track again
git update-index --no-assume-unchanged path/to/file
git update-index应该做你想做的
这将告诉 git 您要开始忽略对文件的更改
git update-index --assume-unchanged path/to/file
当您想再次开始跟踪时
git update-index --no-assume-unchanged path/to/file
回答by wjl
Another solution is to use a pre-commit
hook. See git help hooks
for details.
另一种解决方案是使用pre-commit
钩子。详情请参阅git help hooks
。
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.
这更难设置:您必须编写一个 shell 脚本来准确表达您想要允许或禁止的内容,这可能意味着您需要对 git 有相当好的掌握。
它更持久和灵活:有一些技巧,比如
git update-index
你可能会不小心忘记运行它等等。但是一旦你设置了一个钩子,你就再也不用担心了。
Like the other tricks suggested, hooks don't automatically get propagated between repositories, but you cancheck them in as regular files and have everyone link them into .git/hooks
.
与建议的其他技巧一样,钩子不会自动在存储库之间传播,但您可以将它们作为常规文件检入,并让每个人将它们链接到.git/hooks
.