A combination of potential blog posts, links, stackoverflow answers and experience. Ctrl+F is your friend.

Git

Delete history

Use the following script / commands. $1 refers to the commit hash of the commit from which you would like to keep history.

#!/bin/bash 
git checkout --orphan temp $1 # create a new branch without parent history 
git commit -m "Truncated history" # create a first commit on this branch, note that this might result in a lot of new untracked files (can also delete unwanted items here) 
git rebase --onto temp $1 master # now rebase the part of master branch that we want to keep onto this branch 
git branch -D temp # delete the temp branch 

# The following 2 commands are optional - they keep your git repo in good shape. 
git prune --progress # delete all the objects w/o references 
git gc --aggressive # aggressively collect garbage; may take a lot of time on large repos

Don't forget to git push -f to force push changes to master at the end.

Clean up a git repository

First clean up all branches and stashes that you don't require. git branch -D andgit push -d <remote_name> <branch_name>

git fetch --prune # prune remote branches 
git stash clear 
git reflog expire --expire-unreachable=now --all 
git gc --prune=now
Remove untracked files
  • files: git clean -n
  • files: git clean -f
  • directories: git clean -fd
  • ignored files: git clean -fX
  • ignored and non-ignored files: git clean -fx
Disable local change tracking for certain files

git update-index --assume-unchanged <path> to disable change tracking

git update-index --no-assume-unchanged <path> to enable change tracking

Reset local branch after a force push

Note that this may require a git clean in the local repo

git fetch git reset --hard origin/remote-branch-name
Checkout branch from tag
git checkout tags/v1.0.0 -b bugfix-v1
Cherry pick a commit in staging mode
git cherry-pick <commit-hash> -n
Pretty format git log when tig is not available
git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
Find pending commits between two branches excluding already cherry-pick/merged changes
git cherry -v branch-name master

It lists all the commits that are on master but not on branch-name with a leading + mark each line. If the line starts with a - mark, the following commit (of master) has an equivalent commit on branch-name (via merge or cherry-pick), which can be ignored (Reference).

Another option:

git log --no-merges --cherry-pick branch-name..master

The above lists out commits that are on master but not on the branch. However note that since the commit id of a cherry picked commit changes, the relevant patch will still show up as missing in the git log result if it has already been cherry picked.

Finally, after cherry picking missing patches and resolving conflicts, make sure to merge the relevant branches to not have to follow the white rabbit again :)

Git fetch and pull a remote branch

For a simple fast-forward merge use:git fetch <remote> <sourceBranch>:<destinationBranch>. Examples,

  • Merge local branch foo into local branch master, without having to checkout master first. Here `.` means to use the local repository as the "remote": git fetch . foo:master
  • Merge remote branch origin/foo into local branch foo, without having to checkout foo first:git fetch origin foo:foo
Git cleanup local branches

The following command grabs all branches that have been merged into master, filters byfeat and deletes them:

  • git branch --merged origin/master | grep "^\s*feat/" | xargs git branch -d
  • git branch --merged origin/develop | grep -v "develop\|master" | xargs git branch -d

Remove the filter if you'd like to live precariously.

Javascript

Docker

  • Build image: docker build -t [image-name] .
  • Run image in detached mode and expose docker port 25 as 3035 on localhost:docker run -d -p 3025:25 [image-name]
  • List running docker images: docker ps
  • Jump into a shell on running docker image: docker exec -it [container-id] bash, (change bash to any other command to execute a command on the running instance) orsudo docker exec -i -t [container-id] /bin/bash
  • Stop all images: docker stop $(docker ps -a -q)
  • Stop all images: docker rm $(docker ps -a -q)
  • Inspect attached volumes: sudo docker inspect -f '{{ .Mounts }}' [container-id]
  • Cleanup all docker images & volumes: docker system prune --volumes
  • Disable auto-start for docker containers (for containers that have been started viadocker-compose): docker update --restart=no [container-id]

Postgresql

cli (psql)
connect to an existing db with username and password
psql <dbname> -h <hostname> -U <username> -W, make sure to uselocalhost when necessary
create db and user via root
sudo -u postgres psql postgres 
CREATE ROLE myuser LOGIN PASSWORD 'mypass'; 
CREATE DATABASE mydatabase WITH OWNER = myuser; 
SHOW port;
psql -h localhost -d mydatabase -U myuser -p <port>
create db and user programmatically
sudo -u postgres createuser -s $USER createdb mydatabase psql -d mydatabase
show all users
\du
show tables
\dt,SELECT table_name FROM information_schema.tables WHERE table_schema = 'public';
show databases
\l, SELECT datname FROM pg_database;
show columns
\d table,SELECT column_name FROM information_schema.columns WHERE table_name ='table';
describe table
\d+ table,SELECT column_name FROM information_schema.columns WHERE table_name ='table';
quit
\q
Extract `bytea` data into a file, an image for example
Use `COPY` command with encode to hex format and then apply `xxd` shell command (with `-p` continuous hexdump style switch):\copy (SELECT encode(file, 'hex') FROM samples LIMIT 1) TO '~/image.hex'xxd -p -r ~/image.hex > ~/image.jpg

Encryption

Check SSL expiration date

echo | openssl s_client -showcerts -connect smalldata.tech:443 2>/dev/null | openssl x509 -inform pem -noout -dates

Encrypt a file
# Encrypt 
$ openssl enc -a -aes-256-cbc -in plain.txt -out encrypted.enc 
 
# Decrypt 
$ openssl enc -d -a -aes-256-cbc -in encrypted.enc -out plain.txt
  • -d to decrypt
  • -pass to specify a password source. This argument can have various formats:pass:password to specify the password directly in the command, env:var to read it from the environment variable $var, file:pathname to read it from the file at pathname, fd:number to read it from a given file descriptor, andstdin to read it from standard input (equivalent to fd:0, but NOT equivalent to reading it from the user's terminal, which is the default behavior if-pass is not specified)
  • -a to base64-encode the encrypted file (or assume it's base64-encoded if decrypting)

Ubuntu

Email

echo 'hello snoop' | mail -aFrom:vic@smalldata.tech -s "Test subject" snoop@gmail.com

Java

sudo update-java-alternatives

Disable terminal bell

https://www.cyberciti.biz/faq/how-to-linux-disable-or-turn-off-beep-sound-for-terminal/

a) Click on Applications > Accessories > Terminal
b) Now click on Edit Menu > Current Profile
c) Click on General Tab > General > Remove check box (Terminal bell)

You can also remove the driver: sudo modprobe -r pcspkr. Append the following line to your /etc/modprobe.d/blacklist so that beep remains off after the system reboot:blacklist pcspkr

Disable touchscreen
xinput --list
xinput disable [touchscreen XID]
Edit nautilus bookmarks

There is a really weird bug with my current nautilus setup that does not allow me to simply click on a nautilus bookmark - the item always ends up being dragged. Therefore, editing bookmarks is pretty much impossible via the GUI. They can be manually edited here: ~/config/gtk-3.0/bookmarks

CLI
Count lines of code

find . -name '*.js' | xargs wc -l

PDF & Image manipulation
Conversion
convert -density 300 taxi.pdf -quality 90 taxi.jpg 
convert taxi.jpg taxi.pdf 
convert *.jpg wedding-party-invite.pdf
Downscaling scanned pdfs
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf
  • -dPDFSETTINGS=/screen lower quality, smaller size. (72 dpi)
  • -dPDFSETTINGS=/ebook for better quality, but slightly larger pdfs. (150 dpi)
  • -dPDFSETTINGS=/prepress output similar to Acrobat Distiller "Prepress Optimized" setting (300 dpi)
  • -dPDFSETTINGS=/printer selects output similar to the Acrobat Distiller "Print Optimized" setting (300 dpi)
  • -dPDFSETTINGS=/default selects output intended to be useful across a wide variety of uses, possibly at the expense of a larger output file
Check dpi of images

identify -format "%w x %h %x x %y" card.png wherePixelsPerInch = PixelsPerCentimeter * 2.54

making a gif with resize

convert -resize 50% -delay 20 -loop 0 *.JPG myimage.gif

resize images

for file in *.JPG; do convert $file -resize 50% lo-$file; done

extract pages from pdf

pdftk full-pdf.pdf cat 12-15 output pages.pdf

Convert raw camera images to jpeg
sudo apt install darktable
find . -type f ( -iname "*.orf" -o -iname "*.raw" ) -exec sh -c 'darktable-cli {} ${0%.*}.jpeg' {} ; -delete

Remove delete at the end to not delete the original.

Ebooks
Convert from epub to pdf using calibre
ebook-convert src.epub out.pdf --extra-css "body { font-family: Lato, Georgia; line-height: 1.5; }"  --base-font-size 10 --embed-all-fonts --margin-left 20 --margin-right 20 --margin-top 20 --margin-bottom 20 --unsmarten-punctuation
Firefox
  • Always on top: super + right click on tab bar to show ubuntu application menu.