简体   繁体   English

git:忽略生产分支中的源和开发中的缩小文件

[英]git: ignore source in production branch and minified files in development

I cannot find a sensible solution for as I reckon a very simple task. 我无法找到合理的解决方案,因为我认为这是一个非常简单的任务。 Consider I have some source directory in my project, where all source files are. 考虑我的项目中有一些source目录,其中包含所有源文件。 In development mode, I change source files, and Gulp minifies them into .min.js and .min.css and puts them into public directory (to clarify, I'm building a node.js app). 在开发模式下,我更改源文件,Gulp将它们缩小为.min.js.min.css并将它们放入public目录(澄清一下,我正在构建一个node.js应用程序)。

Now, I have two branches: master for production and development for development. 现在,我有两个分支:生产和development master

.gitignore file on master branch master分支上的.gitignore文件

.DS_Store
node_modules
source
...

.gitignore file on development branch development分支上的.gitignore文件

.DS_Store
node_modules
public/javascript
public/stylesheets
...

The behavior I want to get: 1) ignore minified files for development (they are assembled and minified every time I change the code anyway), but keep source; 我想要获得的行为:1)忽略缩小的文件以进行开发(每次我更改代码时它们都会被组合和缩小),但保留源代码; 2) ignore source files for production (since I don't want to have them when I $ git pull it into the production environment), but keep minified. 2)忽略用于生产的源文件(因为我不希望在我$ git pull它带入生产环境时使用它们),但要保持缩小。

For some reason when I switch back and forth from master to development , git completely removes some files and breaks everything. 出于某种原因,当我从master设备切换到development ,git会完全删除一些文件并破坏所有内容。 For instance, it wipes all contents of every module in node_modules , forcing me to do $ rm -rf node_modules && npm install every time I switch branches. 例如,它擦除了node_modules中每个模块的所有内容,强迫我每次切换分支时都执行$ rm -rf node_modules && npm install

Any help is very much appreciated. 很感谢任何形式的帮助。


The problem is also described here: Using git, how do I ignore a file in one branch but have it committed in another branch? 这里还描述了这个问题: 使用git,我如何忽略一个分支中的文件但是在另一个分支中提交它? , but with no working solutions. ,但没有可行的解决方案。

It feels to me like you're asking the wrong question. 我觉得你问的是错误的问题。 The minified scripts are "build products", not "source code". 缩小的脚本是“构建产品”,而不是“源代码”。 Seems to me like some sort of "make install" process should create them, rather than having them in source control. 在我看来,某种“make install”过程应该创建它们,而不是让它们在源代码控制中。 But I don't work in the web space so I do not know what standard practice is. 但我不在网络空间工作,所以我不知道标准做法是什么。

One solution would be to avoid switching back and forth between the branches (avoiding the "breaks everything" part) 一种解决方案是避免在分支之间来回切换(避免“破坏一切”部分)

From your repo currently checked out as master , do (with git 2.5+) a: 从目前签出master仓库中,做(使用git 2.5+)a:

git worktree add ..\dev dev

Using the new git worktree command , you will get a second working tree, linked to your cloned (only once) repo. 使用新的git worktree命令 ,您将获得第二个工作树,链接到您的克隆(仅一次)repo。
See " Multiple working directories with Git? ". 请参阅“ 使用Git多个工作目录? ”。

In both worktrees (one for each branch) you maintain your .gitignore as you see fit. 在两个工作树(每个分支一个)中,您可以根据需要维护.gitignore

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

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