简体   繁体   中英

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. 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).

Now, I have two branches: master for production and development for development.

.gitignore file on master branch

.DS_Store
node_modules
source
...

.gitignore file on development branch

.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; 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.

For some reason when I switch back and forth from master to development , git completely removes some files and breaks everything. 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.

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? , 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. 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:

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.
See " Multiple working directories with Git? ".

In both worktrees (one for each branch) you maintain your .gitignore as you see fit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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