software engineering

Remove branch from remote git repo

By Adam K Dean on

To remove a branch from a remote git repository, you have to push an empty branch.

$ git push origin :branch-name
- [deleted]        branch-name

If you want to also remove it from the local repo, delete the branch:

$ git checkout master 
Switch to branch 'master'

$ git branch -D branch-name
Deleted branch branch-name (was abcd123)

We change branch as you can't delete the branch you're on.

RegEx match all words with a prefix

By Adam K Dean on

Here is a snippet to match all words that begin with a specified prefix.


JavaScript implementation:

"test tbl_test1 tbl_test2 test".match(/\btbl_\S+/g)


/\btbl_\S+/g.exec("test tbl_test1 tbl_test2 test")

Which is the same as:

var regex = /\btbl_\S+/g;
    matches = [],

while (match = regex.exec(line)) {

If you want a dynamic prefix, use RegExp:

var regex = new RegExp('\\b' + prefix + '\\S+', 'g'),
    matches = [],

while (match = regex.exec(line)) {

Fleetctl destroy all services

By Adam K Dean on

Here is a nice little bash trick to destroy all services using fleetctl.

fleetctl list-units | sed 1d | while read -r line ; \
    do fleetctl destroy $(echo $line | cut -f1 -d ' '); done

It gets a list of units, removes the first line, loops through, grabs the first word, and and destroys it.

$ fleetctl list-units | sed 1d | while read -r line ; \
    do fleetctl destroy $(echo $line | cut -f1 -d ' '); done
Destroyed Job august-frosting_v4.web.1-announce.service
Destroyed Job august-frosting_v4.web.1-log.service
Destroyed Job august-frosting_v4.web.1.service
Destroyed Job august-frosting_v4.web.2-announce.service
Destroyed Job august-frosting_v4.web.2-log.service
Destroyed Job august-frosting_v4.web.2.service
Destroyed Job quaint-teamwork_v5.cmd.1-announce.service
Destroyed Job quaint-teamwork_v5.cmd.1-log.service
Destroyed Job quaint-teamwork_v5.cmd.1.service
Destroyed Job quaint-teamwork_v5.cmd.2-announce.service
Destroyed Job quaint-teamwork_v5.cmd.2-log.service
Destroyed Job quaint-teamwork_v5.cmd.2.service

Now imagine typing each of those by hand.

Count occurrences with grep

By Adam K Dean on

Today I learnt that grep has a '-c' switch that counts occurrences. I'll never pipe it to 'wc -l' again!

$ ps aux | grep -c adam

But if we use wc, we would also need to trim it:

$ ps aux | grep adam | wc -l

So, use -c.

Scale a Dockerfile app in Deis

By Adam K Dean on

Deis allows you to run apps in two different ways: using a Procfile, and using a Dockerfile. When using a Procfile, you define what type of processes you'll be running, such as:

web: node index.js

To scale this, say to 2 instances, it's as simple as:

$ deis ps:scale web=2

This doesn't work when using the Dockerfile approach however. For that, we can see from ps:list that the instance type is actually cmd.

$ deis ps:list
--- cmd:
cmd.1 up (v2)

If we were using a Procfile, this would say web.1 up (v2) etc. So to scale the Dockerfile, we need to use cmd rather than web.

$ deis ps:scale cmd=2

And then, once it's got coffee, it'll scale your apps.