How to restart gulp watch when git branch changes

It's useful to have gulp generate files using a task watch

. In my case, I have a task that keeps track of a directory php

for changes and lints / copy on change, a JS directory that merges and copies, and SASS that compiles to CSS on change.

If I checkout another git branch running for hours, it quite expectedly goes crazy as there are literally hundreds of files changing. One problem that can be solved is that it watch

runs common tasks - PHP for example, just redraws and copies all PHP files, not just the ones that have changed. So if 50 files change, the entire stack is overflowed 50 times. Ditto for JS (due to dependencies) and SASS (because it runs a compass that also sees every changed file).

So, currently my solution is to kill my task watch

(which works with Sublime Gulp ) and then check out a new one and run it again. I would suggest that any solution would be to either change the plugin Sublime Gulp

or stop me using it - it would be nice if there was a quick shortcut to have the task watch

run in the background of my terminal, and let me see the output, but not force me.

I know that git has hooks, and so I guess another solution might be that the checkout hook creates a file that can act as a temporary stop sign for the view task, or something similar.

Has anyone experienced a similar problem using watch

and how would you suggest solving it?


source to share

2 answers

As stated in @ Mushr00m's answer for Gulp.js, the view function runs twice when saving files , there is an option debounceDelay

you can pass to


debounceDelay {integer} Delay for events called sequentially for the same file / event

In short, as long as all your changes happen at the same time, debounceDelay

will wait before running your viewed tasks. In my experience, git is usually pretty fast, so you can install debounceDelay

perhaps 500

or 1000

without having much impact on your development workflow.

Example:'/**/*.less', {debounceDelay: 2000}, ['less']);


I was able to verify that this needs to be digged in the documentation. The actual functionality comes from gaze .



I am using forever-monitor to set up child processes for my monitors, and I figured out I can watch .git/HEAD

as a trigger to restart my monitors.

My code looks like this:

return watch('./.git/HEAD', {name: 'Branch watcher'}, function(events, done) {
  monitors.forEach(function(monitor) {


You can easily replace my control with monitor

any other code to execute on branch change:

return watch('./.git/HEAD', {name: 'Branch watcher'}, function(events, done) {
  // Do what needs to be done on branch change


If you have another watch viewer, it will be difficult to restart it without using something like forever-monitor

, because the system-level kill gulp processes will kill your branch viewer. I installed this though, and it works very smoothly.



All Articles