Sam Johnston - Senior Software Engineer

Sam Johnston

Senior Software Engineer
AWS, NodeJS, React

Thursday, 16 January 2020

Home » Articles » Useful Developer Snippets

Useful Developer Snippets

Useful Developer Snippets

Some useful JS/bash/docker/git snippets that I have collected over the years

Rewriting Git History

git lol
# copy the sha of the commit before the mistake
git rebase COMMIT_SHA -i
# edit the commits and complete the rebase
push -f

List Processes Using A Specific Port

kill -9 PID_NUMBER

Run locally installed npm executable without installing globally

Cypress Example:
cd node_modules/.bin/cypress open

Run Jasmine Tests Locally

npm i http-server -g
http-server .

Run Jasmine Tests Headless

npm install phantomjs-prebuilt -g
phantomjs spec/javascripts/run-jasmine2.js spec/javascripts/SpecRunnerRequire.html

Run A JS File In The Console

function addScript( src ) {
  var s = document.createElement( ‘script’ );
   s.setAttribute( ‘src’, src );
   document.body.appendChild( s );


Dive into a Docker Container

ssh IP_ADDRESS -i ~/.ssh/ansible-key
docker ps | grep SERVICE_NAME
docker exec -ti DOCKER_PID sh

Prevent a Page Reload

addEventListener('beforeunload', () => {

Run a JavaScript bookmarklet

You may need to create a bookmark first and edit, but you can change it's location to create a JS script that runs on a page. (Can be done on a mobile device).

javascript: document.cookie = 'cookie-name=cookie-value;path=/';

Adding ssh Keys to an Existing EC2 Instance

Useful for granting access to SFTP, especially if there are multiple users on the instance (not ec2-user)

  1. Create a new key pair in the EC2 Console, and download it.
  2. Connect to the instance using the AWS browser terminal.
  3. Create a ~/.ssh/authorized_keys file in the user directory you want to access.
  4. Locally, retrieve the public key by running ssh-keygen -y -f /path_to_key_pair/my-key-pair.pem
  5. Paste the output into the authorized_keys file you just created.
  6. You can now use the key pair to connect locally. You'll need to use the correct username, default is ec2-user

Modified: Friday, 16 April 2021