8

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.

Community
  • 1
  • 1
Anton Egorov
  • 1,328
  • 1
  • 16
  • 33
  • 1
    Ох, Антошка... кто ж тебя учил-то так через задницу все делать?! Выходи в местный чат, переговорим за грехи твои тяжкие – Lazy Badger Apr 16 '16 at 15:18
  • @LazyBadger никто не учил, Сашка, вот сам мучаюсь-страдаю, как же правильно-то сделать! – Anton Egorov Apr 16 '16 at 19:04
  • I am afraid there is no simple solution. Maybe you can write some scripts to automate such processes. – gdlmx Apr 25 '16 at 21:08
  • 3
    It feels to me like you're asking the wrong problem. 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. – Mort Apr 26 '16 at 17:44
  • @Mort it seems like this is the most sensible answer so far. As I saw this issue, there should be a `development` branch, where all development happens, and there should be a `production` branch, the "quintessence" of the development process, which should be deployed as-is. However, it seems like the best approach is to have this `production` branch as the last, stable version of the code, push it to the server where it gets executed and do this `make install` compilation, probably as a post-merge hook. – Anton Egorov Apr 26 '16 at 20:07
  • @Mort can you post it as an answer? :) – Anton Egorov May 01 '16 at 12:37

2 Answers2

4

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.

Mort
  • 3,379
  • 1
  • 25
  • 40
3

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.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250