忽略跟踪的文件更改
Ignore tracked file changes
我有一个由 git 跟踪的文件 config.txt
,它是在 git clone
上下载的,但我不希望任何人更改和提交它,即如果有人更改它文件我不希望他们的更改被跟踪因此提交和意外推送。
我仍然希望以后能够添加 config.txt
的潜在更改,但是要以明确的方式(可以是 git add -f config.txt
或其他方式)。
此外,执行此操作的额外步骤也是不可取的,因此 git update-index --skip-worktree
或 git update-index --assume-unchanged
不是一个足够好的解决方案,因为它容易出现人为错误。
是否可以在 git 中这样做?
该问题的一个可能解决方案是在克隆存储库后“跳过”跟踪文件:
git update-index --skip-worktree <path-name>
缺点是这需要所有用户在克隆存储库后执行额外的步骤,因此您可以将其添加到 README
您不能忽略对 Git 中跟踪文件的更改。 Git FAQ explains:
Git doesn’t provide a way to do this. 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. 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 跟踪的文件 config.txt
,它是在 git clone
上下载的,但我不希望任何人更改和提交它,即如果有人更改它文件我不希望他们的更改被跟踪因此提交和意外推送。
我仍然希望以后能够添加 config.txt
的潜在更改,但是要以明确的方式(可以是 git add -f config.txt
或其他方式)。
此外,执行此操作的额外步骤也是不可取的,因此 git update-index --skip-worktree
或 git update-index --assume-unchanged
不是一个足够好的解决方案,因为它容易出现人为错误。
是否可以在 git 中这样做?
该问题的一个可能解决方案是在克隆存储库后“跳过”跟踪文件:
git update-index --skip-worktree <path-name>
缺点是这需要所有用户在克隆存储库后执行额外的步骤,因此您可以将其添加到 README
您不能忽略对 Git 中跟踪文件的更改。 Git FAQ explains:
Git doesn’t provide a way to do this. 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. 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.
如果您的目标是使用配置文件,那么最好的办法是添加示例或模板文件,然后让用户将其复制到位或让脚本创建适当的文件。然后您应该忽略实际配置文件的位置,只检查示例或模板。