[英]Ignore tracked file changes
I have a file config.txt
that is tracked by git, it is downloaded on git clone
, but I don't want anybody changing and committing it ie if someone changes that file I don't want their changes to be tracked thus committed & accidentally pushed.我有一个由 git 跟踪的文件
config.txt
,它在git clone
上下载,但我不希望任何人更改并提交它,即如果有人更改该文件,我不希望他们的更改被跟踪因此提交 &不小心推了。
I still want to be able to add the potential changes of config.txt
later on, but in an explicit way(could be git add -f config.txt
or something else).我仍然希望稍后能够添加
config.txt
的潜在更改,但是以明确的方式(可能是git add -f config.txt
或其他)。
Also an extra step for doing this is not desirable so git update-index --skip-worktree
or git update-index --assume-unchanged
is not a good enough solution as it is prone to human error.这样做的额外步骤也是不可取的,因此
git update-index --skip-worktree
或git update-index --assume-unchanged
不是一个足够好的解决方案,因为它容易出现人为错误。
Is it possible to do so in git?是否可以在 git 中这样做?
You cannot ignore changes to a tracked files in Git.您不能忽略对 Git 中跟踪文件的更改。 The Git FAQ explains :
Git 常见问题解答说明:
Git doesn't provide a way to do this.
Git 没有提供执行此操作的方法。 The reason is that if Git needs to overwrite this file, such as during a checkout, it doesn't know whether the changes to the file are precious and should be kept, or whether they are irrelevant and can safely be destroyed.
原因是如果 Git 需要覆盖这个文件,例如在结帐时,它不知道对文件的更改是否宝贵并且应该保留,或者它们是否无关紧要并且可以安全地销毁。 Therefore, it has to take the safe route and always preserve them.
因此,它必须采取安全路线并始终保护它们。
It's tempting to try to use certain features of git update-index, namely the assume-unchanged and skip-worktree bits, but these don't work properly for this purpose and shouldn't be used this way.
尝试使用 git 更新索引的某些功能是很诱人的,即假设不变和跳过工作树位,但这些不能正常工作,不应该以这种方式使用。
If your goal is to work with a configuration file, then the best thing to do is add an example or template file and then either have the user copy it into place or have a script create the appropriate file.如果您的目标是使用配置文件,那么最好的做法是添加示例或模板文件,然后让用户将其复制到位或让脚本创建适当的文件。 You should then ignore the location of the actual configuration file and only check in the example or the template.
然后,您应该忽略实际配置文件的位置,只签入示例或模板。
One possible solution to that problem is to "skip" tracking the file after the repository is cloned:该问题的一种可能解决方案是在克隆存储库后“跳过”跟踪文件:
git update-index --skip-worktree <path-name>
The downside is that this requires an additional step from all users after cloning the repo, so you could add that in your README
不利的一面是,这需要所有用户在克隆存储库后执行额外的步骤,因此您可以将其添加到您的
README
中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.