简体   繁体   English

Git忽略对文件的更改,但将其保存在存储库中

[英]Git ignore changes to file but keep it in the repo

This is a git question concerning our C# Xamarin project. 这是关于我们的C#Xamarin项目的git问题。

We have a couple files in an Assets folder that are copied in there by a pre-build event command. 我们在Assets文件夹中有几个文件,这些文件通过预构建事件命令复制到其中。 The files are not used directly by the compiler but if they are missing our CI server will fail the build. 编译器不会直接使用这些文件,但是如果缺少这些文件,我们的CI服务器将使构建失败。 The versions of them that are on all the dev machines are probably different so whenever we build on our machines the files get copied in and git shows that they are changed 所有开发机器上它们的版本可能会有所不同,因此,每当我们在机器上构建文件时,文件都会被复制并git显示它们已更改

It's annoying because changes to these files really don't matter so we never need to commit them. 令人讨厌的是,对这些文件的更改实际上并不重要,因此我们永远不需要提交它们。 I want to ignore changes to these files but not remove them from the repo. 我想忽略对这些文件的更改,但不希望将它们从存储库中删除。 If they are removed as mentioned above CI will fail. 如果如上所述将它们删除,则CI将失败。 If I straight up "remove and stop tracking" them they will be deleted from the CI server when it pulls. 如果我整理“删除并停止跟踪”,则将它们从CI服务器中拉出时将其删除。

From other stack overflow questions I have tried both of these: 从其他堆栈溢出问题中,我尝试了以下两种方法:

git update-index --assume-unchanged [path]
git update-index --skip-worktree [path]

Both succeed in hiding the file changes but both present the following problem. 两者都成功隐藏了文件更改,但是都存在以下问题。 When I try to switch branches it tells me I must discard unstaged changes before switching and lists those files. 当我尝试切换分支时,它告诉我必须在切换之前放弃未分段的更改并列出这些文件。 I then type: 然后输入:

git reset -- .
git checkout .

Both fail to discard the changes to the files until I reverse the update-index commands, only then can I discard the changes and be allowed to switch branches. 在我撤消update-index命令之前,这两种方法都不能放弃对文件的更改,只有这样我才能放弃更改并允许切换分支。

Is there a solution to this? 有针对这个的解决方法吗? Are we doing something outside the box or just plain wrong? 我们是在框外做某事还是只是犯错?

This is a very common problem. 这是一个非常普遍的问题。

Sometimes it's the well known custom.js , custom.min.js , custom.css , custom.min.css , and the likes. 有时是众所周知的custom.jscustom.min.jscustom.csscustom.min.css等。 Other times it's another kind of "source files" that somehow needs to be "there" in order for software to work, but we dont really care about what's in them. 其他时候,它是另一种“源文件”,为了使软件能够正常工作,它们必须以某种方式存在,但我们并不真正在意它们中的内容。

Sounds familiar? 听起来很熟悉? It's called compiled files. 称为编译文件。 Now I know they are not actually compiled files - they are source files. 现在我知道它们实际上不是编译文件-它们是源文件。 But for all intents and puposes these files are pretty much the same as compiled files. 但是出于所有目的和目的,这些文件与编译文件几乎相同。 They are not really anything you care about, and would rather the computer just figured out how to maintain them. 它们并不是您真正关心的任何东西,而是计算机只是想出了如何维护它们。

Why treat them any different, than actual compiled files? 为什么将它们与实际的编译文件区别对待? You mention a pre-build event command makes these files every time you build. 您提到每次构建时,预构建事件命令都会使这些文件生成。 That practically makes them part of the build process. 这实际上使它们成为了构建过程的一部分。

The solution is rather straightforward. 解决方案非常简单。 On the CI server include the pre-build event that generates the files, before the actual compiling, and add the files to the .gitignore file. 在CI服务器上,包括在实际编译之前生成文件的pre-build事件,并将这些文件添加到.gitignore文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM