.gitignore是否具有等效于版本控制文件的文件? [英] Does .gitignore have an equivalent for version controlled files?

查看:55
本文介绍了.gitignore是否具有等效于版本控制文件的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该问题与以下问题相关: https://stackoverflow.com/search?q=[git]+%2Bassume不变,但是,这些问题提供的答案似乎都不满足我的需求.

This question is related to the following questions: https://stackoverflow.com/search?q=[git]+%2Bassume-unchanged, however, none of the provided answers to these questions seem to meet my needs.

我有一个由python脚本生成的头文件(key_info.h).聪明起见,我用一行代码控制了标头(key_info.h)的占位符版本:

I have a header file (key_info.h) that gets generated by a python script. To be clever, I version controlled a placeholder version of the header (key_info.h) with a single line of code:

int i = you_need_to_run_the_key_info_gen_python_script_to_generate_this_file();

在运行python脚本之前,编译此代码将产生一个错误,告诉开发人员确切的操作;运行脚本.

Prior to running the python script, compiling this code will yield an error telling the developer exactly what to do; run the script.

自然,开发人员将继续运行python脚本以生成最新版本的key_info.h,并且代码将成功编译.然后,开发人员将继续进行一系列更改,然后"git commit"他们的代码.

Naturally, the developer will proceed by running the python script to generate the latest version of key_info.h and the code will successfully compile. The developer will then go on to make a series of changes and then 'git commit' their code.

我们遇到的问题是key_info.h现在已被修改.这样,git会将其作为修改并允许其提交.但是,此标头的生成版本永远不应受版本控制.我们需要将占位符版本保持不变,以继续向开发人员提供编译错误提示.

The problem we run into is the fact that key_info.h has now been modified. As such, git will pick it up as a modification and will allow it to be committed. However, the generated version of this header should never be version controlled. We need the placeholder version to remain unchanged to continue to provide the compile error hint to developers.

我的第一个直觉是使用.gitignore来防止git在提交中获取key_info.h.由于.gitignore本身是版本控制的,因此开发人员之间可以轻松共享.gitignore.不幸的是,.gitignore仅适用于不受版本控制的文件.

My first instinct was to use .gitignore to prevent git from picking up key_info.h in a commit. .gitignore is then easily shared between developers since .gitignore itself is version controlled. Unfortunately, .gitignore only works for files that aren't version controlled.

我的下一个想法是使用'git update-index --assume-unchanged/path/to/file'.这在本地效果很好,但是我无法对其他开发人员强制执行此规则.开发人员意外提交生成的标头只是时间问题.

My next thought was to use 'git update-index --assume-unchanged /path/to/file'. This works great locally, but I have no way of enforcing this rule upon other developers. It's only a matter of time before a dev accidentally commits the generated header.

所以我的问题是:有没有一种方法可以对文件进行版本控制,同时静默执行一个规则,即在提交更改时要忽略该文件?我真正想要的是用于版本控制文件的.gitignore.

So my question is this: is there a way to version control a file, while silently enforcing a rule that the file is to be ignored when it comes time to commit changes? What I really want is .gitignore for version controlled files.

推荐答案

但是我无法对其他开发人员强制执行此规则

but I have no way of enforcing this rule upon other developers

几乎可以做到:您可以使您的key-info-gen脚本运行git update-index --assume-unchanged /some/path.

You do, almost: you could make your key-info-gen script run git update-index --assume-unchanged /some/path.

但是我认为这不是一个好主意.

But I don't think this is a good idea.

我可能会选择完全不覆盖头文件.让您的Python脚本生成正确的头文件并将其写入不同目录,并以正确的顺序传递这两个目录的-I编译器选项.如果脚本尚未运行,则将找到版本控制的标头.如果脚本已运行,则将找到用户生成的标头.然后,您可以将用户生成的标头的路径放在.gitignore中.

I would probably opt for not overwriting the header file at all. Let your Python script generate the correct header file and write it into a different directory, and pass -I compiler options for both of those directories, in the right order. If the script has not been run, the version-controlled header will be found. If the script has been run, the user-generated header will be found. And you can then put the path of the user-generated header in .gitignore.

这篇关于.gitignore是否具有等效于版本控制文件的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆